简体   繁体   English

如何防止通过意图将数据传递给多个活动?

[英]How to prevent passing data to multiple activities through intent?

I have an activity A that passes data to activity B through Intent. 我有一个活动A,它通过Intent将数据传递给活动B。 Now Activity B calls Activity C and Activity C calls Activity D. Now Activity D is calling activity B. Now activity B check for the intent data but the Activity D doesn't pass any data. 现在,活动B调用活动C,活动C调用活动D。现在活动D调用活动B。现在活动B检查意图数据,但活动D不传递任何数据。

Now one way to do this is to pass data through C and D and finally get it in B but i would like to know if there is a better way to handle this ? 现在,一种方法是通过C和D传递数据,最后在B中获取数据,但是我想知道是否有更好的方法来处理此问题?

i would like to add that the data can be an object with a lot of variables and array. 我想补充一点,数据可以是具有很多变量和数组的对象。

Edit :- The data is necessary for activity B to work. 编辑:-数据对于活动B起作用是必需的。

Thanks in advance. 提前致谢。

Use SharedPreferrence; 使用SharedPreferrence;

For storing values; 用于存储值;

SharedPreferences sp=getSharedPreferences("Login", Context.MODE_PRIVATE);
SharedPreferences.Editor Ed=sp.edit();
Ed.putString("id",login_id.getText().toString() );
Ed.putString("password",login_password.getText().toString() );
Ed.commit();

For getting values; 为了获得价值;

SharedPreferences sharedPreferences = getSharedPreferences("Login", MODE_PRIVATE);
String uName = sharedPreferences.getString("id", "");
String password = sharedPreferences.getString("password", "");

You can Check Your Intent is not null and its has Extras value or not by getting Bundle and another check if Bundle oBject is not null then you check keys are there or not in Bundle Object. 您可以通过获取Bundle来检查您的Intent是否为null且是否具有Extras值,然后再次检查Bundle oBject是否为null,然后检查Bundle Object中的键是否存在。

Intent intent = getIntent();
 if(intent!=null)
 {
   Bundle bundle =  intent.getExtras();
   if(bundle!=null)
   {
      if(bundle.containsKey("key"))
      {
      //here you can put your code for Actvity B when comes from Activity  A.
      }
   }
 } 

EDIT 1 Use singleTask launchMode in Activity B in manifest file. 编辑1在清单文件的活动B中使用singleTask launchMode。

android:launchMode="singleTask" 

You can use gson.jar to store class objects into SharedPreferences. 您可以使用gson.jar将类对象存储到SharedPreferences中。

add GSON dependency in Gradle file: 在Gradle文件中添加GSON依赖项:

compile 'com.google.code.gson:gson:2.7'

Creating a shared preference: 创建共享首选项:

SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);

To save: 保存:

  Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(MyObject);
prefsEditor.putString("MyObject", json);
prefsEditor.commit();

To Retrieve: 检索:

    Gson gson = new Gson();
String json = mPrefs.getString("MyObject", "");
MyObject obj = gson.fromJson(json, MyObject.class);

If the data you are passing is different, then assign a variable (named activity_name ) with the name of the activity that is calling the intent. 如果您传递的数据不同,则为变量(名为activity_name )分配一个调用该意图的活动的名称。 In this way, you can check if you are coming to activity B from activity A or D. 这样,您可以检查您是从活动A还是活动D进入活动B。

Let me try to explain with an example: 让我尝试用一​​个例子来解释:

ActivityA 活动A

String activity_name = "ACTIVITY_A";

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("calling_activity", activity_name);
intent.putExtra("your_data", your_data); 
startActivity(intent);

ActivityB 活动B

Intent intent = getIntent();
String callingActivity = intent.getExtras().getString("calling_activity");

if (callingActivity.matches("ACTIVITY_A")) {
    // Do something
} else if (callingActivity.matches("ACTIVITY_D")) {
    // Do something else
}

and in ActivityD 并在ActivityD中

String activity_name = "ACTIVITY_D";

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("calling_activity", activity_name);
// No passed data in Actiivty D
startActivity(intent);

This same logic can be employed using SharedPreferences as remarked in other answers and comments. 如其他答案和注释中所述,可以使用SharedPreferences使用相同的逻辑。

write below code in your activity B. 在活动B中编写以下代码。

String str= getIntent().getStringExtra("key");

if (str == null){
str= "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM