Question

I 'm calling getParent().setResult(0) in TabActivity. This is called when the user hits previous button. I want the current activity to close and get deleted from the stack.

getparent() returns null. Can someone tell me why does that happen??

getParent().setResult(0);
finish();

Thanks

UPDATE: This is the definition of getParent()...What does embedded child mean. And secondly is the TabActivity an embedded child if it is called from another Activity??

public final Activity getParent ()
Return the parent activity if this view is an embedded child.
Was it helpful?

Solution

You haven't clearly stated you're question. You want to understand how to pass a result back from an Activity to the Activity which called it. You must first understand that Activities aren't hierarchical even though they are kept on a back stack. Later activities do not belong to the Activity they are called from.

However, here is the answer the question you meant to ask:

You are using startActivityForResult(Intent, int) (Which you can read up on here)

When Activity A calls startActivityForResult() method Activity B is started, this should do whatever processing is required and then when it exits either call:

setResult(RESULT_OK)

when a yes/no is required or

setResult(RESULT_OK, intent)

where intent is an Intent which contains bundled information you want to use in Activity A to act upon. After Activity B exits Activity A will resume and call the method:

 protected void onActivityResult(int requestCode, int resultCode, Intent data)

This is where you will process the result.

You can read all about this here:

http://developer.android.com/reference/android/app/Activity.html#StartingActivities

OTHER TIPS

If you don't want to keep this Activity in the history stack do one of the following:

a) when starting the TabActivity in the parent, add flag: FLAG_ACTIVITY_NO_HISTORY

Intent intent = new Intent(this, TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, requestCode);

b) OR add android:noHistory="true" in the activity's Manifest like so

    <activity
        android:name=".TabActivity"
        android:noHistory="true"
        ...
        >
        ...
    </activity>

Then to return the result to the parent and finish TabActivity

Intent result = new Intent();
result.putExtra("somevalue", requestCode);
setResult(Activity.RESULT_OK, result); // or setResult(Activity.RESULT_CANCELED, result);
finish();

When you finish(); the TabActivity activity it will not be remembered

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top