从一个活动传递到另一个数据,只好到其他活动后 [英] Pass data from one activity to another, BUT go to the other activity later
问题描述
我的工作流程如下:
LoginActivity - > ActivityB - > ActivityC - > ActivityD
LoginActivity -> ActivityB -> ActivityC -> ActivityD
我想从LoginActivity到ActivityD传递数据,而不是直接去ActivityD。也就是说,我想从LoginActivity数据传递到ActivityD,只好到ActivityB和ActivityC之前,我要ActivityD和获取数据存在。
I'd like to pass data from LoginActivity to ActivityD, but not go directly to 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.
这可能吗?
我知道从一个活动传递数据到另一个code是如下:
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);
而在 AnotherActivity
执行以下操作来获取数据:
And in AnotherActivity
you do the following to get the data:
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String var = extras.getString("something");
}
但是,这并不工作,如果我想要延迟直行到活动中。所以,如果我拿出 startActivity(我);
并转到另一个活动。 tyring获取数据的最后一项活动时,程序崩溃。好老NullPointerExeption弹出。
But this does not work if I want to delay going straight to the activity. So if I take out startActivity(i);
and go to another activity. The program crashes when tyring get the data in the final activity. The good old NullPointerExeption pops up.
有谁知道做什么,我所描述的方法吗?从一个活动到另一个获取数据,而不是去(或启动)的活动,对吗?
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?
推荐答案
如果你想通过这样的数据,它是preferable使用共享preferences。 它是简单的使用,你可以在你的程序中使用数据的任何地方。
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);
这篇关于从一个活动传递到另一个数据,只好到其他活动后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!