返回按钮和刷新先前的活动 [英] Back button and refreshing previous activity

查看:53
本文介绍了返回按钮和刷新先前的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有两项活动:

  1. 文件列表和上次修改时间
  2. 文件编辑活动

用户从列表中选择一个文件,然后转到文件编辑活动.编辑完成后,用户按后退"按钮返回文件列表.

A user selects a file from the list and is taken to the file editing activity. When done editing, the user presses the back button to return to the file list.

该列表未重新加载,因此对于刚编辑的文件修改时间,显示的值不正确.

The list is not reloaded and therefore an incorrect value is displayed for the just edited files modified time.

按下后退按钮后导致文件列表刷新的正确方法是什么?

What is the proper method of causing the file list to refresh after the back button is pressed?

此示例假定不使用数据库,仅使用ArrayAdapter.

This example assumes that no database is being used, just an ArrayAdapter.

推荐答案

一种选择是使用第一个活动的onResume.

One option would be to use the onResume of your first activity.

@Override
public void onResume()
    {  // After a pause OR at startup
    super.onResume();
    //Refresh your stuff here
     }

或者您可以启动结果活动":

Or you can start Activity for Result:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

如果要发送回数据,请在secondActivity中

In secondActivity if you want to send back data:

 Intent returnIntent = new Intent();
 returnIntent.putExtra("result",result);
 setResult(RESULT_OK,returnIntent);     
 finish();

如果您不想返回数据:

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();

现在,在FirstActivity类中,为onActivityResult()方法编写以下代码

Now in your FirstActivity class write following code for onActivityResult() method

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == 1) {

     if(resultCode == RESULT_OK){      
         //Update List         
     }
     if (resultCode == RESULT_CANCELED) {    
         //Do nothing?
     }
  }
}//onActivityResult

这篇关于返回按钮和刷新先前的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆