简体   繁体   English

如何反压传递活动?

[英]How to backpress pass an activity?

I have an activity that loads some content using an AsyncTask. 我有一个使用AsyncTask加载一些内容的活动。

If the content is null then i launch an activty that contains a WebView to load the data. 如果内容为null,那么我启动一个包含WebView的微软来加载数据。

The only problem is when i launch the activity using regular intents. 唯一的问题是当我使用常规意图启动活动时。

When the back button is pressed to return out of the WebView. 按下后退按钮返回WebView。

It returns to the following activity and the AsyncTask is ran again, and it does the same thing all over again. 它返回到以下活动,并再次运行AsyncTask,它再次执行相同的操作。

I know how to Override the onBackPressed button. 我知道如何覆盖onBackPressed按钮。 But what should i do to override this from happening each time? 但是我应该怎么做才能在每次发生这种情况时超越这种情况呢?

If the content is null then when you launch the second Activity (with the WebView) you should call finish() on the first Activity. 如果内容为null,那么当您启动第二个Activity(使用WebView)时,您应该在第一个Activity上调用finish()

EDIT: Alternatively you should use startActivityForResult and then in the onActivityResult method invoke finish() . 编辑:或者你应该使用startActivityForResult ,然后在onActivityResult方法中调用finish()

If you don't want to go back to the that loads the content, then call its finish() method after launching the WebView activity. 如果您不想返回加载内容的内容,则在启动WebView活动后调用其finish()方法。 The back button will then exit the app (or go to the activity before the loading one.) 后退按钮将退出应用程序(或在加载之前转到活动。)

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

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