简体   繁体   中英

How to send info to mainActivity back from Activity 2 without killing Activity 2

I have a second activity that the users can customize and thus i can't destroy it when the user goes back to the main activity(the information needs to be there if the user comes back to the second activity). Also when the user returns to the main activity i need to send some information back(as of right now, a string) but that is not working. My code:

Main Activity :

    public void goToSecondPage(){
          Intent intent = new Intent();
          intent.setClass(this,secondActivity.class);
          startActivity(intent);
    }
    protected void onRestart(){
          super.onRestart();
          Bundle extras = getIntent().getExtras();

          if (extras != null){
                String result = extras.getString("CONFIG");
    }

Second Activity :

    public void goBackToMainPage(){
          Intent resultIntent = new Intent();
          resultIntent.setClass(this,MainActivity.class);
          resultIntent.putExtra("CONFIG","Test String");
          resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(resultIntent);

    }

I'm aware of the onActivityResult method,but it seems that i need to destroy the second activity to use it. How can i have a second activity that can't be destroyed AND can send information back to the main if necessary?

You should not keep data in the activity. You can have only one active activity and the system can kill everything else no matter what you do. You should store the data in another place - SharedPrefference , SQLite Database or Singleton class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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