简体   繁体   English

Android中的儿童活动

[英]Child Activity in Android

So I have two Activities. 所以我有两个活动。 The main is called Main , and the child one is called Child . 主要叫做Main ,孩子叫做Child When a button is clicked in the main activity it triggers the following piece of code: 在主活动中单击按钮时,它会触发以下代码:

Intent i = new Intent(Main.this, Child.class);
Main.this.startActivity(i);

That opens the Child activity. 这打开了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. 只要我调用finish()或按子活动中的后退按钮而不是返回主要活动,应用程序就会关闭。 Can you give me a hint where the problem might be :( 你能给我一个暗示问题可能是:(

PS By trial and error I found out that if edit AndroidManifest.xml and add PS通过反复试验我发现如果编辑AndroidManifest.xml并添加

android:theme="@android:style/Theme.Dialog"

within the declaration of Child the back button and calling finish() behaves as expected: closes the child activity and brings the main into focus. Child的声明中,后退按钮和调用finish()按预期运行:关闭子活动并使main成为焦点。 The problem is that when I start typing in an EditText the screen starts flickering (rather bizzare). 问题是当我开始在EditText中输入时,屏幕开始闪烁(而不是古怪)。 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 onPause event is fired and it stops the camera until onResume is called. 虽然启动子活动时,会触发onPause事件并在调用onResume之前停止摄像头。

Edit: 编辑:

So I tried using startActivityForResult and added 所以我尝试使用startActivityForResult并添加

Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();

to the onPause and a similar one to the onResume methods. onPauseonResume方法类似的一个。 When the Child returns onResume doesn't get triggered. 当Child返回onResume时不会被触发。 I even overrided onActivityResult and even that doesn't get triggered. 我甚至凌驾于theActivityResult上 ,甚至没有被触发。 :( So bizarre... :(太奇怪......

I think I found the problem but I can't solve it myself 我想我发现了问题,但我自己无法解决

When the Child activity is activated, onStop and immediately after that onDestroy are invoked within the Main activity. 激活活动时, onStop以及在活动中调用onDestroy之后立即激活。 But why?!? 但为什么?!?

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方法中放置断点或打印语句,以查看正在调用的方法。

According to 根据 http://developer.android.com/resources/faq/commontasks.html#opennewscreen what happens when you launch the new Activity is indeed different: http://developer.android.com/resources/faq/commontasks.html#opennewscreen启动新Activity时会发生什么情况确实不同:
If the first Activity is still visible somehow (as you discovered, if it's shown as dialog for example), it will just receive onPause() ; 如果第一个Activity仍以某种方式显示(如您所发现的,如果它以对话框形式显示),它将只接收onPause() ; if it's not visible at all anymore, it will receive onStop() too. 如果它根本不可见,它也会收到onStop()
But as the other said, if you're launching the second activity to get results from it, startActivityForResults seems more logical 但正如另一个所说,如果你要启动第二个活动来获得结果, 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个空格来缩进它们,显然)

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

相关问题 android-意图活动的子活动 - android - child activity for intent ones 按下Android后背无法进行多个孩子活动 - Android on Back Pressed for multiple child activity not working 无法在子活动Android Studio中保存数据 - Unable to save data in child activity android studio Android:仅在父活动中的子活动上调用函数 - Android: call a function only on child activity from parent activity Android:父Activity和两个子Activity类中的一个静态TextView - Android: a static TextView in a parent Activity and two child Activity classes that manipulate it Android子视图在父级活动的onActivityResult中为空 - Android child view being null within onActivityResult in parents activity 在Android Java编程中如何从其祖先那里看到子活动? - How to see child Activity from its ancestor, in Android Java Programming? 在子活动中按下返回按钮后,Android主要活动的元素不响应 - Android main activity's elements do not respond after pressing the back button from child activity android体系结构:从活动中调用dao方法并将结果传递给子活动以进行过滤 - android architecture: call dao method from activity and pass result to child activity for filtering 将父活动的自定义对话框覆盖到所有视图,包括android中的子活动 - To overlay a custom dialog for parent Activity to all views including child activity in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM