简体   繁体   中英

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 ?

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);
}

Don't bother with setting the parent, just handle the up button yourself. Here is how;

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
        case android.R.id.home: 
           // Launch activity here
           return(true);
     }
    return(super.onOptionsItemSelected(item)); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM