简体   繁体   English

通过按返回按钮重新启动上一个活动

[英]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'. 我正在制作一个应用程序,其中有一个“ MainActivity.class”,其中包含一个“编辑配置文件”按钮,该按钮会导致另一个活动,称为“ Editprofile.class”。 It (MainActivity) also has a TextView which displays a string (name). 它(MainActivity)也有一个TextView,它显示一个字符串(名称)。

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. Editprofile.class接受来自用户的字符串,并在按下“添加”按钮后,新的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; 现在,更新后,用户通过后退按钮返回MainActivity时,我希望TextView显示新的更新后的String; for which I have to restart MainActivity. 为此,我必须重新启动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. 当用户在“编辑配置文件”活动期间按下“编辑配置文件”按钮并在用户按下“后退”按钮时,通过使用finish()函数已经实现了这一点,MainActivity通过onBackPress()方法重新启动。

Code for intent to start 'Editprofile.class': 用于启动“ Editprofile.class”的代码:

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

Code to go back to 'MainActivity': 返回“ 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?? 我应该以Q / A格式发布吗?

But I also wanted to 'refresh' the MainActivity so that the newly updated name will be displayed on going back; 但我也想“刷新” MainActivity,以便新的名称在返回时显示。

There's the onResume() method for just this sort of thing. 这类事情有onResume()方法。

I recommend using startActivityForResult() and updating your MainActivity in onActivityResult() . 我建议使用startActivityForResult()并在onActivityResult()更新MainActivity

(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 .) (无需检查onResume()的更新,因为除了从Editprofile返回之外, 许多其他实例还调用了此方法。)

An alternative to @Mattias answer is to start Editprofile using startActivityForResult(...) . @Mattias答案的替代方法是使用startActivityForResult(...)启动Editprofile 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 . 然后,该活动可以返回一个结果,该结果指示用户是否实际上已经编辑了他/她的个人资料,您可以在MainActivity onActivityResult(...)中捕获该个人资料。 Only if the profile changed the TextView needs to be updated. 仅当配置文件更改时,才需要更新TextView

It may take a few lines of extra coding, but is slightly more efficient than updating every single time onResume() gets hit. 可能需要花费几行额外的编码,但比每次onResume()命中时更新都要有效。 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. 例如,请查看Android开发人员网站上的文档

Just: 只是:

  • remove the finish() call in edit_profile() 删除edit_profile()中的finish()调用
  • remove the entire onBackPressed() method 删除整个onBackPressed()方法

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() . onResume()而不是onCreate()更新TextView。

Don't Use/override onBackPressed() anywhere in your program 不要在程序中的任何地方使用/覆盖onBackPressed()

just add the following method in the parent activity such as MainActivity and EditProfile 只需在父活动(例如MainActivityEditProfile添加以下方法

In MainActivity.java 在MainActivity.java中

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

Start Activity on your onClick method using 使用onClick方法启动Activity

    startActivityForResult(intentName, 0);

In EditProfile.java 在EditProfile.java中

you use/override onStop() method 您使用/覆盖onStop()方法

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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