简体   繁体   English

将数据从一个活动传递到另一个活动,但稍后将 go 传递到另一个活动

[英]Pass data from one activity to another, BUT go to the other activity later

My work flow is as follows:我的工作流程如下:

LoginActivity -> ActivityB -> ActivityC -> ActivityD LoginActivity -> ActivityB -> ActivityC -> ActivityD

I'd like to pass data from LoginActivity to ActivityD, but not go directly to ActivityD.我想将数据从 LoginActivity 传递到 ActivityD,而不是将 go 直接传递到 ActivityD。 ie, I'd like to pass data from LoginActivity to ActivityD, but go to ActivityB and ActivityC before I get to ActivityD and get the data there.即,我想将数据从 LoginActivity 传递到 ActivityD,但在到达 ActivityD 并在那里获取数据之前将 go 传递到 ActivityB 和 ActivityC。

Is this possible?这可能吗?

I know to pass data from one activity to another the code is as follows:我知道要将数据从一个活动传递到另一个活动,代码如下:

        Intent i = new Intent(getApplicationContext(), AnotherActivity.class); 
        i.putExtra("key", (int)1); 
        i.putExtra("something", something); 
        startActivity(i);

And in AnotherActivity you do the following to get the data:AnotherActivity中,您执行以下操作来获取数据:

        Bundle extras = getIntent().getExtras();   
        if(extras !=null) 
        {  
            String var = extras.getString("something");
        }

But this does not work if I want to delay going straight to the activity.但是,如果我想延迟直接进行活动,这将不起作用。 So if I take out startActivity(i);所以如果我取出startActivity(i); and go to another activity.和 go 到另一个活动。 The program crashes when tyring get the data in the final activity.当在最终活动中尝试获取数据时,程序崩溃。 The good old NullPointerExeption pops up.旧的 NullPointerExeption 弹出。

Does anyone know a way of doing what I've described?有谁知道做我所描述的事情的方法? Get the data from one activity to another, but not go (or start) that activity right away?将数据从一项活动获取到另一项活动,但不是立即 go(或开始)该活动?

If you want to pass the data like this, it is preferable to use shared preferences.如果你想像这样传递数据,最好使用共享首选项。 It is simple to use and you can use the data any where in your program.它使用简单,您可以在程序的任何地方使用数据。

SharedPreferences.Editor editor = prefs.edit();
editor.putString("key", (int)1);
editor.putString("something", something);
editor.commit(); 

at the time of receiving:接收时:

SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
String something = prefs.getString("something", null);

You can always use any of these for it您可以随时使用其中任何一个

•A singleton pattern to share the date across multiple non-consecutive activities.

•A shared preference.

•An external storage

•Entry into db.

•passing data with intents.

This link will give you some more details.链接将为您提供更多详细信息。

I can think of two options to solving your problem.我可以想到两种方法来解决您的问题。 One is that you just keep passing the data, which means in ACtivityB and ACtivityC you need to run the code一种是您只是不断传递数据,这意味着在 ACtivityB 和 ACtivityC 中您需要运行代码

Intent i = new Intent(getApplicationContext(), AnotherActivity.class); 
i.putExtra("key", (int)1); 
i.putExtra("something", something); 
startActivity(i);

The other option (which I think is more practical) is to create a seperate class with static members and static methods for accessing the data.另一种选择(我认为更实用)是创建一个单独的 class 和 static 成员和 static 方法来访问数据。 It could look something like this它可能看起来像这样

public class SharedResource{
public static String mPassedName;
}

This class would not necessarily be threadsafe so be forewarned, if you have mutlithreaded access take the appropriate precautions.这个 class 不一定是线程安全的,所以请注意,如果您有多线程访问,请采取适当的预防措施。

You can do it by two approaches.你可以通过两种方法来做到这一点。 1 ) Shared Preferences 1 ) 共享偏好

SharedPreferences.Editor editor = prefs.edit();
editor.putString("key", "Value");
editor.commit(); 

// fetching Shared Pref. //获取共享首选项。

  String value = pref.getString("key" , "");

2 ) Defining application level class and access same. 2 ) 定义应用级别 class 并访问相同。

Class Consisting of getter and setter functions for variables. Class 由变量的 getter 和 setter 函数组成。 setting in one activity and getting into another activity.设置一项活动并进入另一项活动。

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

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