将活动中的数据发送到另一个而不启动它 [英] send data from activity to another without start it
问题描述
如果我有两个活动Activity1和Activity2,并且我想在不启动Activity2的情况下将数据从Activity1发送到Activity2
If I have two activities Activity1 and Activity2 and I want to send data from Activity1 to Activity2 without Start Activity2
我知道是否要启动Activity2,我在Activity1.java中使用此代码
I know if I want to start Activity2 I use this code in Activity1.java
Intent intent ;
Bundle bundle = new Bundle();
bundle.putString(BG_SELECT, hexColor);
intent = new Intent(getApplicationContext(), Activity2.class);
intent.putExtras(bundle);
// this is to start but I want just refresh Activity2 not start it
startActivityForResult(intent, uniqueNo);
和Activity2.java
and in Activity2.java
bundle = getIntent().getExtras();
if (bundle != null) {
bgColor = bundle.getString(SebhaActivity.BG_SELECT);
System.out.println("in Activity2, selected BG: "+bgColor);
}
如何在不启动Activity2的情况下刷新Activity2以在其中查找数据? 预先感谢.
How to refresh Activity2 to find data in it without Start it? Thanks in Advance.
推荐答案
如果下一个活动(您需要数据,即Activity2的地方)不会从此处开始,则可以将数据保存在活动1的SharedPreferences中,并在活动2时访问它您到达那里
if the next activity (where you need data, Activity2 i.e) wont start from here you can save the data in SharedPreferences in Activity 1 and access it in activity2 when you get there
这篇关于将活动中的数据发送到另一个而不启动它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!