简体   繁体   中英

Restarting the previous activity by pressing back button

I'm making an app in which there is a 'MainActivity.class' which has a button 'Edit profile' that leads to another activity, called 'Editprofile.class'. It (MainActivity) also has a TextView which displays a string (name).

Editprofile.class accepts a string from user and upon pressing a button 'add', the new String gets updated to the database, replacing the previous string. Now when, after updating, the user goes back to the MainActivity via the back button I want the TextView to display the new updated String; for which I have to restart MainActivity.

I have achieved this by using finish() function when the user presses the 'Edit profile' button and when the user presses the back button during the Editprofile activity, the MainActivity gets restarted via onBackPress() method.

Code for intent to start 'Editprofile.class':

 public void edit_profile(View view) { Intent intent = new Intent(this, Editprofile.class) startActivity(intent) finish(); } 

Code to go back to 'MainActivity':

 @Override public void onBackPressed() { finish(); startActivity(new Intent(this, MainActivity.class)); } 

I have managed to get the result I wanted but I would appreciate it if someone can tell me if it is the most efficient method.

Should have I posted this in a Q/A format??

But I also wanted to 'refresh' the MainActivity so that the newly updated name will be displayed on going back;

There's the onResume() method for just this sort of thing.

I recommend using startActivityForResult() and updating your MainActivity in onActivityResult() .

(There is no need to check for an update in onResume() , since this method is called by many other instances than just returning from Editprofile .)

An alternative to @Mattias answer is to start Editprofile using startActivityForResult(...) . That activity can then return a result indicating whether the user has actually edited his/her profile, which you'll be able to capture in onActivityResult(...) of MainActivity . Only if the profile changed the TextView needs to be updated.

It may take a few lines of extra coding, but is slightly more efficient than updating every single time onResume() gets hit. In my opinion it also a bit cleaner, although in terms of performance it probably won't be a big deal.

For an example, have a look at the documentation on the Android developers website.

Just:

  • remove the finish() call in edit_profile()
  • remove the entire onBackPressed() method

As you don't need to restart the activity (now it will be resumed which is: faster, better, easier and as intended). Update the TextView in onResume() instead of onCreate() .

Don't Use/override onBackPressed() anywhere in your program

just add the following method in the parent activity such as MainActivity and EditProfile

In MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

Start Activity on your onClick method using

    startActivityForResult(intentName, 0);

In EditProfile.java

you use/override onStop() method

    @Override
protected void onStop()
{
    super.onStop();

}

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