Kaushik S Kaushik S - 2 months ago 45
Android Question

How to set parent activity of an activity programmatically in android?

I have an activity 'A' in the application that I'm developing. This activity will be shown once I start the app. It can also be visited by selecting it from the overflow menu in the action bar.

I have configured the action bar of this activity 'A' in such a way that the

UP
icon will be displayed when it is selected from overflow menu and not when the activity appears during starting of the app.

But, this UP icon, when clicked, does not do anything because I have not set the parent activity in the manifest file. If the parent activity is set in the manifest file, I'm getting the UP icon in the same activity which appears when the app is started.

Flow:
StartApp-> Activity A (without Up icon) -> Home Screen -> Overflow menu -> Activity A (with up icon).


How do I set the parent activity for activity 'A' when Up icon appears ?

Answer

You can handle the up click and fire off your parent activity intent:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
        startActivity(new Intent(this, YourActivity.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}