Câu hỏi phỏng vấn Android
Câu hỏi

How do I pass data between Activities in Android application?

Câu trả lời

Here's how you can pass data between Activities in an Android application:

Using Intents

  1. In the sending Activity, create an Intent and put the data you want to pass using putExtra() method. For example:
java Copy
Intent intent = new Intent(this, ReceivingActivity.class);
intent.putExtra("key", "Value to pass");
startActivity(intent);
  1. In the receiving Activity, retrieve the data from the Intent using getIntent() and getStringExtra() (or the appropriate method based on the data type). For example:
java Copy
String value = getIntent().getStringExtra("key");

Using Bundles

  1. Create a Bundle in the sending Activity and put the data you want to pass using putString(), putInt(), etc. For example:
java Copy
Bundle bundle = new Bundle();
bundle.putString("key", "Value to pass");
intent.putExtras(bundle);
  1. In the receiving Activity, retrieve the data from the Bundle using getBundle() and getString() (or the appropriate method based on the data type). For example:
java Copy
Bundle bundle = getIntent().getExtras();
String value = bundle.getString("key");

Using SharedPreferences

  1. Store the data in SharedPreferences in the sending Activity. For example:
java Copy
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Ed...
junior

junior

Gợi ý câu hỏi phỏng vấn

senior

What is Broadcast Receiver?

middle

When to use Android Loaders?

junior

What is the most appropriate way to store user settings in Android application?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào