重新启动 Android 中的 Activity

发布于 2024-12-11 13:36:00 字数 147 浏览 0 评论 0原文

我有一项活动 A,它有一个按钮和一个显示书籍名称的列表视图。单击按钮后,活动 B 开始,用户填写图书表格并保存。当他按下后退按钮时,用户来到活动 A。这里应该在列表视图中更新书名。我想我必须在 onResume() 中编写一些代码。你能告诉我要写什么吗?我正在使用自定义列表视图。

I have one activity A, that has one button and one list view which shows names of books . on click of the button, activity B starts, there user fill the book form and save it . when he press back button , user comes to activity A. Here the book name should be updated in listview. I think I have to write some code in onResume() . Can u please tell me what to write. I am using customised list view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗失的美好 2024-12-18 13:36:00

使用 startActivityForResult() 启动 Activity B 并使用方法 onActivityResult() 重新启动或处理新数据

例如,启动 Activity B:

String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);

然后在 Activity A 类中的某个位置:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == 0){
            // do processing here
        }
    }

其他答案应该足够了,但是在通过其他方式恢复活动的情况下可以调用 onResume() 。

要在用户从 Activity B 按下后退按钮时简单地重新启动 Activity A,然后将以下内容放入 onActivityResult 中:

if(requestCode == 0){
            finish();
            startActivity(starterintent);

        }

并在 Activity A 的 onCreate 中添加 starterintent = getIntent();

只需记住初始化变量在调用 onCreate 之前使用 Intent starterintent;

例如

public class ActivityA extends ListActivity {
  Intent starterintent;

  public void onCreate(Bundle b){
    starterintent = getIntent();
  }

  protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == 0){
      finish();
      startActivity(starterintent);
    }
  }

  private void startActivityB(){
    String callingActivity = context.getLocalClassName();
    Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
    newActivity.setData(Uri.parse(callingActivity));
    startActivityForResult(newActivity, 0);
  }

}

然后只需通过单击按钮或其他方式调用 startActivityB()

Start activity B with startActivityForResult() and use method onActivityResult() to restart or process the new data

For example, to start Activity B:

String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);

Then somewhere in your Activity A class:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == 0){
            // do processing here
        }
    }

The other answers should suffice, but onResume() can be called in cases where the activity is resumed by other means.

To simply restart Activity A when user presses back button from Activity B, then put the following inside the onActivityResult:

if(requestCode == 0){
            finish();
            startActivity(starterintent);

        }

And in the onCreate of Activity A, add starterintent = getIntent();

Just remember to initiate the variable with Intent starterintent; somewhere before your onCreate is called.

e.g.

public class ActivityA extends ListActivity {
  Intent starterintent;

  public void onCreate(Bundle b){
    starterintent = getIntent();
  }

  protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == 0){
      finish();
      startActivity(starterintent);
    }
  }

  private void startActivityB(){
    String callingActivity = context.getLocalClassName();
    Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
    newActivity.setData(Uri.parse(callingActivity));
    startActivityForResult(newActivity, 0);
  }

}

Then just call startActivityB() from a button click or whatever

握住我的手 2024-12-18 13:36:00

是的你是对的。在onResume中编写代码。

当您更新日期时,只需调用 notifyDataSetChanged();为您的 ListView 适配器

希望,它对您有帮助!

YES you are right. Write code in onResume.

When you updated date just call notifyDataSetChanged(); for your ListView adapter

Hope, it help you!

情定在深秋 2024-12-18 13:36:00

您可以在用户按“保存”时启动活动,它会为您修复它。
或者如果您想按回:

@Override
public void onResume(){
    super.onResume();
    list.clear();
    list.addAll(getBooks());
    adapter.notifyDataSetChanged();
}

You can either start the activity when user press on Save, and it will fix it for you.
Or if you want to press back:

@Override
public void onResume(){
    super.onResume();
    list.clear();
    list.addAll(getBooks());
    adapter.notifyDataSetChanged();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文