如何处理再次进入前台的活动

发布于 2024-12-06 11:41:10 字数 1271 浏览 0 评论 0原文

我拥有的书籍在解释如何使用生命周期方面非常糟糕,我遗漏了很多东西,希望有人能够填写。

我的应用程序结构是,当它第一次启动时,它会启动一个充满法律废话的活动,用户必须接受。当他说“确定”时,我开始主要活动,然后像这样调用 finish:

public void onClick(View view) { //as a result of "I accept"
    Intent mainIntent = new Intent(mParent, EtMain.class);
    startActivity(mainIntent); // Start the main program
    finish();
}

然后在 onCreate 方法的 EtMain 中,我有一些选项卡并实例化一些类:

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mTabHost = (TabHost)findViewById(android.R.id.tabhost);
SetupTabs.setMyTabs(mTabHost, this);

mComData = new ComFields(this); // Create the objects
mDepWx = new WxFields(this, DepArr.Departure);
mArrWx = new WxFields(this, DepArr.Arrival);
mDepVs = new DepFields(this);
mArrVs = new ArrFields(this);
mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
}

问题: 第一个片段中的“完成”应该终止 legalbabble 活动,这样它就永远不会重新启动,对吧?即使我的应用程序被推到后台,EtMain 也将永远保留(直到被外部杀死),对吗?

现在的情况是,当 EtMain 被推送并随后带到前台(通过点击图标)时,它会穿过 legalbabble 屏幕,就好像这是一个完整的开始 - 这就是我想阻止的 - 穿过 legalbabble再次屏幕。

看来我想重写第二个代码片段中的 onRestart 并在其中放置一些内容来重新启动应用程序,对吧?这是我不清楚的部分。

我的问题是 onRestart 中需要做什么。我是否必须重新创建所有选项卡和选项卡中的数据以及所有对象实例化?或者应用程序的内存状态是否保存在某个地方,然后恢复到其他东西被带到前台之前的状态,在这种情况下不需要做太多事情,因为所有对象和侦听器仍然在那里?

The books I have are abysmal at explaining how to work with the lifecycle, there's a lot I'm missing that I'm hoping somebody can fill in.

My app structure is that when it's first started, it starts an activity full of legalbabble that the user has to accept. When he says 'ok', I start my main activity and then I call finish like this:

public void onClick(View view) { //as a result of "I accept"
    Intent mainIntent = new Intent(mParent, EtMain.class);
    startActivity(mainIntent); // Start the main program
    finish();
}

Then in EtMain in the onCreate method, I've got some tabs and I instantiate some classes:

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mTabHost = (TabHost)findViewById(android.R.id.tabhost);
SetupTabs.setMyTabs(mTabHost, this);

mComData = new ComFields(this); // Create the objects
mDepWx = new WxFields(this, DepArr.Departure);
mArrWx = new WxFields(this, DepArr.Arrival);
mDepVs = new DepFields(this);
mArrVs = new ArrFields(this);
mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
}

Questions:
The 'finish' in the first fragment should terminate the legalbabble activity so it'll never be restarted, right? And the EtMain one will remain forever (until killed externally), even if my app gets pushed to the background, right?

The way it is now, when EtMain gets pushed and later brought to the foreground (by tapping on the icon), it goes through the legalbabble screen as though it's a complete start - that's what I'd like to prevent - going thru the legalbabble screen again.

It would seem that I'd want to override onRestart in the second code fragment and put something in there to restart the app, right? That's the part I'm unclear about.

My question then is what needs to be done in onRestart. Do I have to recreate all the tabs and data in the tabs and all my object instantiations? Or is the memory state of the app saved someplace and then is restored back to the state that it was in before something else was brought to the foreground in which case not much needs to be done because all the objects and listeners will still be there?

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

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

发布评论

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

评论(3

碍人泪离人颜 2024-12-13 11:41:10
  1. 是的,第一个活动结束后,您不必再次查看该活动。您还可以写入用户的共享首选项以前看过法律信息。

  2. 如果您的 UI 对象创建是在 onCreate 方法中进行的,则该方法应该只调用一次。暂停或恢复不会再次调用 onCreate 方法。

  3. 除非您在 onPause 方法中显式删除对象和 tabChangedListeners,否则不必在 onRestart 方法中触摸它们。

  4. 正确,应用程序的状态会自动保存。您不必触摸 onRestart 方法。

希望这有帮助!

  1. Yes after the first activity has ended you shouldn't have to view that activity again. You could also write to the shared preferences that the user has previously seen legal info.

  2. If you're UI object creation is in the onCreate method, this should only be called once. Pausing or resuming will not call the onCreate method again.

  3. Unless you explicitly remove your objects and tabChangedListeners in the onPause method, you should not have to touch them in the onRestart method.

  4. Correct, the state of the app is saved automatically. You shouldn't have to touch the onRestart method.

Hope this helps!

属性 2024-12-13 11:41:10

我认为问题在于清单中的启动活动是合法的babble活动,因此当您单击该图标时,系统会启动另一个活动。更好的架构是使用 startActivityForResult 从 EtMain 活动的 onCreate 方法中启动 legalbabble 活动。来自文档:

作为一种特殊情况,如果您在初始 onCreate(Bundle savingInstanceState)/onResume() 期间以 requestCode >= 0 调用 startActivityForResult() 您的 Activity,那么您的窗口将不会显示,直到从已启动的 Activity 返回结果为止。

当您在onActivityResult中获得结果时,如果合法内容被拒绝,您可以调用finish();否则一切都会正常进行。

这可以避免在接受合法内容时清单中定义的启动活动完成的问题。

I think the problem is that the launch activity in your manifest is the legalbabble activity, so when you click on the icon, the system launches another one. A better architecture would be to launch the legalbabble activity it from your EtMain activity in the onCreate method of the latter, using startActivityForResult. From the docs:

As a special case, if you call startActivityForResult() with a requestCode >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your activity, then your window will not be displayed until a result is returned back from the started activity.

When you get the result in onActivityResult, you can call finish() if the legal stuff was declined; otherwise everything will proceed normally.

This avoids the problem that the launch activity defined in your manifest finishes when the legal stuff is accepted.

涫野音 2024-12-13 11:41:10

EtMain 不会永远保留,如果用户退出(按 BACK 键),Activity 将完成(onPause,然后 onStop,然后 onDestroy 将被调用)。
一般来说,你可以忽略 onRestore,直到你正在做一些复杂的事情。

一旦用户退出您的应用程序并重新进入(或按主屏幕上的图标),您的第一个 Activity 将会调用 onCreate (随后是 onStart 和 onResume),因此您不需要 onRestart 中的任何逻辑,您的代码在onCreate 会像第一次一样为您进行设置。因此,当用户退出后启动应用程序时,您的法律胡言乱语将再次出现,除非您存储一个首选项(在 SharedPreferences 或数据库或文件中)以表明您已经显示了它 - 在这种情况下立即完成它并启动主程序活动。

仅当应用程序从停止状态(已调用 onStop 但尚未调用 onDestroy)到启动状态(已调用 onStart 但尚未调用 onResume)时,才会调用 onRestart。

为了保存数据 - 某些组件会自动保存其状态(例如 EditTexts 记住其中的文本,TabHosts 记住当前选择的选项卡等)。有些组件不会。如果您希望保存额外的数据,请使用 onSaveInstanceState 和 onRestoreInstanceState。您应该只使用这些方法来恢复应用程序或临时数据的状态,而不是重要的事情,例如用户正在查看的资源的ID,他们所处的缩放级别等。对于诸如联系人或实际数据之类的事情,您应该当调用 onPause 时,应该将这些更改提交到数据库、SharedPreferences 或其他永久存储(例如文件)。

如果您是,我建议您查看 Android Activity 生命周期使困惑。或者问更多问题!

EtMain will not remain forever, if the user backs out (by pressing the BACK key) the Activity will be finished (onPause, then onStop, then onDestroy will be called).
In general you can ignore onRestore until you are doing something complicated.

Once the user has exited your application and re-enters (or presses the icon on the Homescreen), onCreate (followed by onStart and onResume) will be called for your first activity, so you do not need any logic in onRestart, your code in onCreate will do the setting up for you as it did the first time. Because of this your legal babble will appear again when the user starts the app after exiting unless you store a preference (in SharedPreferences or a database or file) to indicate you have already displayed it - in which case finish it straight away and start the main activity.

onRestart is only called when the application goes from the stopped state (onStop has been called but not onDestroy) to the started state (onStart is called but onResume has not yet).

For saving data - some components save their state automatically (e.g. EditTexts remember the text in them, TabHosts remember the currently selected tab etc). Some components will not. If you wish to save extra data then make use of onSaveInstanceState and onRestoreInstanceState. You should only use these methods to restore the state of your application or temporary data, not important things, e.g. the id of the resource what the user was looking at, what zoom level they were at etc. For things like contacts or actual data you should commit these changes to a database, SharedPreferences or other permanent storage (e.g. file) when onPause is called.

I recommend taking a look at the Android Activity lifecycle if you are confused. Or ask more questions!

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