Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child Activity in Android

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:

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

That opens the Child activity.

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 :(

P.S. By trial and error I found out that if edit AndroidManifest.xml and add

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. The problem is that when I start typing in an EditText the 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 onPause event is fired and it stops the camera until onResume is called.

Edit:

So I tried using startActivityForResult and added

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

to the onPause and a similar one to the onResume methods. When the Child returns onResume doesn't get triggered. I even overrided onActivityResult and even that doesn't get triggered. :( 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. But why?!?

like image 201
Martin Marinov Avatar asked Jun 17 '10 16:06

Martin Marinov


People also ask

What is the activity in Android?

An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.

How do I get back the result from child activity to parent in Android?

Intent data = new Intent(); data. putExtra("myData1", "Data 1 value"); data. putExtra("myData2", "Data 2 value"); // Activity finished ok, return the data setResult(RESULT_OK, data); finish();

How do I set parent activity on Android?

Declare a Parent Activity You can do this in the app manifest, by setting an android:parentActivityName attribute. The android:parentActivityName attribute was introduced in Android 4.1 (API level 16). To support devices with older versions of Android, define a <meta-data> name-value pair, where the name is "android.

What is the use of onActivityResult in Android?

When you done with the subsequent activity and returns, the system calls your activity's onActivityResult() method. This method includes three arguments: @The request code you passed to startActivityForResult() . @A result code specified by the second activity.


1 Answers

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).

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.

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.

like image 144
Cheryl Simon Avatar answered Oct 10 '22 00:10

Cheryl Simon