简体   繁体   中英

How to open a section in a tabbed activity using an intent from another activity?

I have a Tabbed Activity with a sidebar that has 4 sections.

When I'm doing an Intent from another activity to this activity, I want it to be open on the fourth section, and not on the first section (the default).

How can I change it to be open on the fourth section?

Try this:

When you send an intent from another activity put a boolean bundle with that intent, when receiving intent on your activity check if bundle is true, if yes open your fourth section.

Your sending activity

Bundle bundle=new Bundle();
bundle.putBoolean("fourthSection",true);

startActivity(new Intent(currentActivity.this,otherActivity.class).putExtras(bundle));

Your receiving activity

Bundle bundle=getIntent.getExtras();
if(bundle.getBoolean("fourthSection")){
//go to your fourth section
}

This is how you'd do it:

public class OtherActivityThatHasYourTabs ... {
  public static final String TAB_INDEX_KEY = "tab_index";
  ...
}  

Have a key named TAB_INDEX_KEY . This is how you'd get and set the tab index to be passed between these two activities.

In your "current" activity, do this:

Intent otherActivity = new Intent(CurrentActivity.this,OtherActivityThatHasYourTabs.class);
otherActivity.putExtra(OtherActivityThatHasYourTabs.TAB_INDEX_KEY,4);
startActivity(otherActivity);  

In your OtherActivityThatHasYourTabs , you can retrieve the Intent used to launch it via getIntent() . So, you'd do this:

Intent intent = getIntent();
Bundle extras = intent.getExtras();
int tabIndex = extras.getInt(TAB_INDEX_KEY);
if( tabIndex != 0 ){
  yourMethodToDisplayTabAtIndex( tabIndex ); 
}  

Start indexing from 1 because getInt() returns 0 if the key passed to getInt() cannot be found.

Using an int gives you more cleanliness and flexibility because you are only managing a single key and you can increment the index if you ever add more tabs.

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