11

If I have two activities Activity1 and Activity2 and I want to send data from Activity1 to Activity2 without Start Activity2

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);

and in Activity2.java

bundle = getIntent().getExtras();

if (bundle != null) {
   bgColor = bundle.getString(SebhaActivity.BG_SELECT);
   System.out.println("in Activity2, selected BG: "+bgColor);

}

How to refresh Activity2 to find data in it without Start it? Thanks in Advance.

4

5 回答 5

15

您可以使用LocalBroadcastManager. 对于您的场景,即 Activity1 向 Activity2 发送内容,您将执行以下操作。

# Activity1

Intent intent = new Intent("INTENT_NAME").putExtra(BG_SELECT, hexColor);
LocalBroadcastManager.getInstance(Activity1.this).sendBroadcast(intent);

在 Activity2 上,您需要首先注册接收器,例如,在其 onCreate 方法上,然后使用 BroadcastReceiver 获取意图,如下所示

# Activity2

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("INTENT_NAME"));
}

然后,注册mReceiver以检索该BG_SELECT字段

# Activity2

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String receivedHexColor = intent.getStringExtra(BG_SELECT);
    }
};
于 2015-11-04T10:38:02.227 回答
7

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

于 2013-02-12T13:42:26.727 回答
1

Try this,

Create one non activity class.

  public class Datas {
  public static String name;
  }

Declare this non activity class in your activity class.

Data  mData = new Data();
String str = mData.name
于 2013-02-12T13:43:27.767 回答
1

If you want to share data between 2 activities while both are running, you should using some storage available for both.

检查Shared Preferences,或创建一个可以在两个活动中访问的全局存储类。没有真正的“方法”来做到这一点。

于 2013-02-12T13:46:30.483 回答
1

我认为这里 SharedPreferences 变得无与伦比。

SharedPreferences 是特定于应用程序的,即执行以下选项之一时数据会丢失: PREFS_NAME 是文件的名称。mode 是操作模式。editor.commit() 用于保存对共享首选项的更改

你的代码是这样的

在您共享数据的第一个活动中

  SharedPreferences preferences=getSharedPreferences("PhoneBook",MODE_PRIVATE);
    SharedPreferences.Editor editor=preferences.edit();

    editor.putInt("registration_id",c.getInt(c.getColumnIndex("db_regiser_id")));
    editor.putBoolean("IsLogin",true);
    editor.commit();

在第二个活动中获取这样的数据

SharedPreferences preferences=getSharedPreferences("PhoneBook",MODE_PRIVATE);
 int registration_id = preferences.getInt("registration_id", 0);

使用 SharedPreferences 是相当安全的。

也访问

https://developer.android.com/training/data-storage/shared-preferences.html

希望这个会有所帮助。

于 2018-03-05T11:59:28.900 回答