Java Android 中的儿童活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3063410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Child Activity in Android
提问by Martin Marinov
So I have two Activities. The main is called Main, and the child one is called Child. When a button is clicked in the main activity it triggers the following piece of code:
所以我有两个活动。主要称为Main,子项称为Child。当在主活动中单击按钮时,它会触发以下代码段:
Intent i = new Intent(Main.this, Child.class);
Main.this.startActivity(i);
That opens the Childactivity.
这将打开Child活动。
As soon as I call finish()or press the back button within the child activity instead of going back to the main one, the app just closes. Can you give me a hint where the problem might be :(
一旦我调用finish()或按下子活动中的后退按钮而不是返回主活动,应用程序就会关闭。你能给我一个提示问题可能出在哪里:(
P.S.By trial and error I found out that if edit AndroidManifest.xmland add
PS通过反复试验我发现如果编辑AndroidManifest.xml并添加
android:theme="@android:style/Theme.Dialog"
within the declaration of Childthe back button and calling finish()behaves as expected: closes the child activity and brings the main into focus. The problem is that when I start typing in an EditTextthe screen starts flickering (rather bizzare). So I can't use it as a dialog. My main activity uses the camera, so that might be making problems. Although when the child activity is started, the onPauseevent is fired and it stops the camera until onResumeis called.
在Child的声明中,后退按钮和调用finish() 的行为符合预期:关闭子活动并使主要活动成为焦点。问题是当我开始输入EditText 时,屏幕开始闪烁(相当奇怪)。所以我不能将它用作对话框。我的主要活动使用相机,因此可能会出现问题。尽管当子活动启动时,onPause事件会被触发并停止相机,直到onResume被调用。
Edit:
编辑:
So I tried using startActivityForResultand added
所以我尝试使用startActivityForResult并添加
Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
to the onPauseand a similar one to the onResumemethods. When the Child returns onResumedoesn't get triggered. I even overrided onActivityResultand even that doesn't get triggered. :( So bizarre...
到onPause和类似的onResume方法。当 Child 返回时onResume不会被触发。我什至覆盖了onActivityResult,甚至没有被触发。:( 太奇怪了...
I think I found the problem but I can't solve it myself
我想我找到了问题,但我自己无法解决
When the Childactivity is activated, onStopand immediately after that onDestroyare invoked within the Mainactivity. But why?!?
当Child活动被激活时,onStop和onDestroy之后立即在Main活动中被调用。但为什么?!?
采纳答案by Cheryl Simon
You should be able to do the following:
您应该能够执行以下操作:
Intent i = new Intent(this, Child.class);
startActivityForResult(i);
(You only need Main.this if you are invoking this from an inner class).
(如果您从内部类调用它,则只需要 Main.this )。
When you want to exit the child activity:
当您想退出子活动时:
setResult(Result.OK);
finish();
This should cause onActivityResult to be invoked in your Main class, followed by OnResume.
这应该会导致在您的 Main 类中调用 onActivityResult,然后是 OnResume。
If that doesn't work, you might try putting break points or print statements in the various onX methods, to see which are being called.
如果这不起作用,您可以尝试在各种 onX 方法中放置断点或打印语句,以查看正在调用哪些方法。
回答by Joubarc
According to http://developer.android.com/resources/faq/commontasks.html#opennewscreenwhat happens when you launch the new Activity is indeed different:
If the first Activity is still visible somehow (as you discovered, if it's shown as dialog for example), it will just receive onPause()
; if it's not visible at all anymore, it will receive onStop()
too.
But as the other said, if you're launching the second activity to get results from it, startActivityForResults
seems more logical
根据 http://developer.android.com/resources/faq/commontasks.html#opennewscreen启动新 Activity 时发生的情况确实不同:
如果第一个 Activity 仍然以某种方式可见(如您所见,如果它显示为例如),它只会收到onPause()
;如果它不再可见,它也会收到onStop()
。
但正如另一个人所说,如果您启动第二个活动以从中获得结果,startActivityForResults
似乎更合乎逻辑
(I'm new and learning formatting too, you may want to read the help by clicking on the orange question mark - use 4 spaces before code samples to indent them, apparently)
(我也是新手,正在学习格式化,您可能想通过单击橙色问号来阅读帮助 - 在代码示例之前使用 4 个空格来缩进它们,显然)