Android 返回时刷新 Activity

发布于 2024-12-18 00:35:48 字数 1028 浏览 0 评论 0原文

我需要一点帮助来刷新我的应用程序中的一项活动。我正在使用选项卡主机活动并连接到 Web 服务并从我的子活动之一下载一些数据。当我在子活动中按下同步按钮时,我将启动一个不在选项卡主机中的新活动,同步完成后,它会返回到其父活动(子活动)。我想要实现的目标是当我返回活动时刷新活动。当我在互联网上检查时,我发现最好的选择是使用 startActivityForResult,但我真的不明白如何使用它以及如何在收到结果时刷新活动完成活动。

如果有人能帮助我,我会很高兴。谢谢!

编辑:

Synchronization.class中显示日志

我正在使用此代码,它甚至没有在 onActivityResult MyCollectionId.class :

Intent intent = new Intent(MyCollectionId.this, Synchronization.class);
intent.putExtra("process", 2);
startActivityForResult(intent, 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if(resultCode==RESULT_OK){
     Log.e("","OnActivityResult");
    Intent refresh = new Intent(this, MyCollectionId.class);
    startActivity(refresh);
    this.finish();
 }
}

Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
setResult(RESULT_OK,intent);
finish();

I need a little help with refreshing one of my activities in my application. I'm using tab host activity and connecting to web service and downloading some data from one of my child activities. When I press sync button in my child activity I'm starting a new activity which is not in tab host and when the sync is done, it returns to it's parent (child activity). The thing that I want to achieve is to refresh the activity when I return back to it. As I checked over the internet I find that the best option to do it is using startActivityForResult,but I don't really understand how to use that and how to refresh the activity when I receive the result from the finished activity.

If anyone can help me, I'll be really glad.Thanks!

EDIT:

I'm using this code and it's not even showing the Log in onActivityResult

MyCollectionId.class :

Intent intent = new Intent(MyCollectionId.this, Synchronization.class);
intent.putExtra("process", 2);
startActivityForResult(intent, 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if(resultCode==RESULT_OK){
     Log.e("","OnActivityResult");
    Intent refresh = new Intent(this, MyCollectionId.class);
    startActivity(refresh);
    this.finish();
 }
}

Synchronization.class :

Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
setResult(RESULT_OK,intent);
finish();

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

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

发布评论

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

评论(7

给我一枪 2024-12-25 00:35:49

另一种棘手的方法是在 onRestart() 上启动您的活动,

@Override
public void onRestart(){
    super.onRestart();
    Intent previewMessage = new Intent(StampiiStore.this, StampiiStore.class);
    TabGroupActivity parentActivity = (TabGroupActivity)getParent();
    parentActivity.startChildActivity("StampiiStore", previewMessage);
    this.finish();
}

这实际上应该可以解决问题。 (在这段代码中,我展示了当您使用自定义 TabActivity 管理器时它的完成方式。)

Another tricky way to do this is just start your activity on onRestart()

@Override
public void onRestart(){
    super.onRestart();
    Intent previewMessage = new Intent(StampiiStore.this, StampiiStore.class);
    TabGroupActivity parentActivity = (TabGroupActivity)getParent();
    parentActivity.startChildActivity("StampiiStore", previewMessage);
    this.finish();
}

That should do the trick actually. (In this code I'm showing the way it's done when you are using a custom TabActivity manager.)

孤檠 2024-12-25 00:35:49

您应该在方法中处理在父活动中以“startActivityForResult”启动的活动的结果:

@override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   //...
}

并且根据结果,您可以再次调用负责显示父活动中的信息的代码(可能是您将其放在进入 onResume() 方法或类似的方法)。

我建议您将负责信息渲染的所有逻辑移至单独的方法中。并在收到结果后调用它。而不是重新启动您的父活动。

You should handle the result of activity that you started with "startActivityForResult" in a parent activity in a method:

@override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   //...
}

And depending on result you could just invoke again a code that is responsible for showing the information in your parent activity (may be you put it into onResume() method or like that).

I would suggest you to move all the logic responsible for information rendering to a separate method. And invoke it after you recieve the result. Instead of restarting your parent activity.

裸钻 2024-12-25 00:35:49

如果您使用 startActivityForResult 方法启动第二个 Activity,则当您返回第一个 Activity 时,将调用第一个 Activity 的 onActivityResult

如果您覆盖它,您可以从那里刷新您的活动。

查看更多信息 这里此处

If you start your second activity with the method startActivityForResult, when you return to the first activity, onActivityResult of the first activity will be called.

If you override it, you can refresh your activity from there.

See more information here and here

没有伤那来痛 2024-12-25 00:35:49

使用带有请求代码的 startActivityForResult 调用子活动,子活动中的 SetResult。当子活动完成后,您可以在 onActivityResult 方法中更新父活动

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

      //Check the result and request code here and update ur activity class

    }

这是一个示例 http://rahulonblog.blogspot.com/2010/05/android-startactivityforresult-example.html

Call child activity using startActivityForResult with request code,SetResult from the child Activity. And when the child activity is finished, you can update you parent activity in onActivityResult method

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

      //Check the result and request code here and update ur activity class

    }

Here is an example http://rahulonblog.blogspot.com/2010/05/android-startactivityforresult-example.html

辞慾 2024-12-25 00:35:49

重写您想要刷新的 Activity 内的 onRestart() 方法,并重新创建该 Activity

@Override
protected void onRestart() {
    super.onRestart();
    this.recreate();
}

Override onRestart() method inside activity you want to refresh, and recreate the activity

@Override
protected void onRestart() {
    super.onRestart();
    this.recreate();
}
念﹏祤嫣 2024-12-25 00:35:49

覆盖

onRestart()

方法,以便当您从所需任务返回时自动调用它。

将以下代码放入 onRestart() 方法中:

@Override
protected void onRestart() {
    super.onRestart();
    finish();
    overridePendingTransition(0, 0);
    startActivity(getIntent());
    overridePendingTransition(0, 0);
}

Override the

onRestart()

method so that it is called automatically when you return back from your desired task.

Put the following code in your onRestart() method :

@Override
protected void onRestart() {
    super.onRestart();
    finish();
    overridePendingTransition(0, 0);
    startActivity(getIntent());
    overridePendingTransition(0, 0);
}
情深缘浅 2024-12-25 00:35:48

按下按钮:

Intent intent = new Intent(this, SyncActivity.class);
        //intent.putExtra("someData", "Here is some data");
        startActivityForResult(intent, 1);

然后在同一个活动类中:

   @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if(resultCode==RESULT_OK){
         Intent refresh = new Intent(this, InitialActivity.class);
         startActivity(refresh);
         this.finish();
      }
     }

同步活动将具有:

setResult(RESULT_OK, null);
finish();

on button press:

Intent intent = new Intent(this, SyncActivity.class);
        //intent.putExtra("someData", "Here is some data");
        startActivityForResult(intent, 1);

Then in the same Activity class:

   @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if(resultCode==RESULT_OK){
         Intent refresh = new Intent(this, InitialActivity.class);
         startActivity(refresh);
         this.finish();
      }
     }

The Sync Activity would have:

setResult(RESULT_OK, null);
finish();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文