send data from activity to another without start it

孤者浪人 提交于 2020-03-21 11:30:31

问题


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.


回答1:


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




回答2:


You can use the LocalBroadcastManager. For your scenario, i.e., Activity1 sends something to Activity2, you would do something like the following.

# Activity1

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

On Activity2, you would need first to register the receiver, for instance, on its onCreate method, then get the intent using a BroadcastReceiver as following

# Activity2

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

Then, register the mReceiver to retrieve the BG_SELECT field

# Activity2

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String receivedHexColor = intent.getStringExtra(BG_SELECT);
    }
};



回答3:


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



回答4:


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

Check the Shared Preferences, or make a global storage class that can be accessed in both activities. There is no real "method" to do it.




回答5:


I think here SharedPreferences is become unbeatable.

SharedPreferences is application specific, i.e. the data is lost on performing one of the following options: PREFS_NAME is the name of the file. mode is the operating mode. editor.commit() is used in order to save changes to shared preferences

Your Code goes like this

In First Activity From where you share data

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

In Second Activity get data like this

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

Use of SharedPreferences is quite safe.

Also visit

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

Hope this one will be helpful.



来源:https://stackoverflow.com/questions/14833840/send-data-from-activity-to-another-without-start-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!