Launching Activities Within a Tab in Android

Launching activities within a tab in Android

It is possible to launch activities within tabs. Therefore set the tabspec content to an ActivityGroup instead of a regular Activity.

tabHost.addTab(tabHost.newTabSpec("Tab")
.setIndicator("Tab")
.setContent(new Intent(this, YourActivityGROUP.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

From within that ActivityGroup you can then start another Activity like this that only updates the contentview of the tab you're in.

class YourActivityGROUP extends ActivityGroup{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//you van get the local activitymanager to start the new activity

View view = getLocalActivityManager()
.startActivity("ReferenceName", new
Intent(this,YourActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
this.setContentView(view);

}
}

How to launch an activity with a specific tab?

You will need to handle it yourself with setCurrentTab in the new activity's constructor.

While calling, you should put additional values in the intent -

Intent i = new Intent(this, MyTabActivity.class);
i.putExtra("FirstTab", 4);

And in constructor of MyTabActivity -

Intent i = getIntent();
int tabToOpen = i.getIntExtra("FirstTab", -1);
if (tabToOpen!=-1) {
// Open the right tab
}

include an activity inside a tab result in full screen activity

You can't have a Activity inside a ViewPager, it must be a fragment.

And also, when you start activity, like this,startActivity(intent) it will not replace the previous screen or activity, it will just create a new activity with a new layout, having the previous activity paused (and also sometimes stopped) until you close the previous activity. You need to learn more about activity and fragment lifecycles

Android - Tabs, MapView, activities within tabs


I see a lot of StackOverflow
questions/answers saying that due to
various limitations in the way the
TabHost is setup, it's best NOT to use
activities as the content of tabs.

As self-appointed President of the Anti-Activity-Tab Alliance (AATA), that's certainly my position.

When an icon corresponding to a
Person, Place, or Event is clicked, it
fires off a VIEW Intent on a URI
corresponding to that object; this is
picked up by an Activity that then
shows the object.

Note that this has nothing to do with having activities as the contents of tabs.

We can launch a new activity to show
the map again, but now we have the map
activity as the content of the tab,
plus the show activity, plus the new
map activity in the activity stack;
given how resource intensive the map
activity is, I'm guessing this is not
the ideal way to go.

I'd avoid it if possible.

I'm worried that if we switch to the
View based way of doing things, we'll
have to do a LOT of housekeeping to
intercept all the back events, try to
switch out the views, etc., etc., as
well as strongly coupling our program
in a way we don't want.

This doesn't follow at all from what you wrote previously. Your "back events" will not change one iota between using Views as the contents of tabs and using Activities as the contents of tabs. Furthermore, this has nothing whatsoever to do with the "loose coupling" pattern you describe -- clicking on an icon in a list in a view in a tab is no different than clicking on an icon in a list in a view in an activity in a tab.

Just have your Show activity tell your, um, main activity to show a particular location, then the Show activity can finish(). The simplest way to do that without introducing a hard JVM coupling between the activities is to broadcast an Intent and register a BroadcastReceiver in the main activity. Upon receipt of this Intent, the main activity would update the map and set it to be the current tab. Of course, this approach is simpler if you have the main activity use Views for its tab contents.

Now, if you try to overhaul your application, such that navigating in a tab doesn't launch another activity, but rather keeps things within its own tab...that is a whole 'nuther kettle of fish.

Start a tab fragments from an activity with the tabs layout

What if you replace everything inside the onClick method of the DialogInterface.OnClickListener with GameLive.this.finish();. This will close the GameLive activity on click and return you to the previous activity, which should be the one that contains the missing navigation bar.



Related Topics



Leave a reply



Submit