Here's the deal. I have an application with three tabs. Through various interactions with the items in the tabs I end up launching other activities. The client has reviewed this and would like the activities launched "within" the tabs, so the tabs remain visible and if the user clicks the tab it goes back to the original activity defined in the setContent function. Is this possible and how would I go about this from other activities? (ie the child activities, not the one that defines the TabHost and has access to call setContent)?
TabLayout is used to implement horizontal tabs. TabLayout is released by Android after the deprecation of ActionBar. TabListener (API level 21). TabLayout is introduced in design support library to implement tabs.
In order to launch one activity from another, you must use an Intent object. After instantiating a new Intent, you simply call the startActivity() method, passing the intent as an argument. The OS will examine the included intent to determine which Activity on the device it should display.
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); } }
Here is my solution
public class ActivityStack extends ActivityGroup { private Stack<String> stack; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (stack == null) stack = new Stack<String>(); //start default activity push("FirstStackActivity", new Intent(this, FirstStackActivity.class)); } @Override public void finishFromChild(Activity child) { pop(); } @Override public void onBackPressed() { pop(); } public void push(String id, Intent intent) { Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); if (window != null) { stack.push(id); setContentView(window.getDecorView()); } } public void pop() { if (stack.size() == 1) finish(); LocalActivityManager manager = getLocalActivityManager(); manager.destroyActivity(stack.pop(), true); if (stack.size() > 0) { Intent lastIntent = manager.getActivity(stack.peek()).getIntent(); Window newWindow = manager.startActivity(stack.peek(), lastIntent); setContentView(newWindow.getDecorView()); } } }
Launch tab
Intent intent = new Intent().setClass(this, ActivityStack.class); TabHost.TabSpec spec = tabHost.newTabSpec("tabId") spec.setContent(intent);
Call next activity
public class FirstStackActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textView = new TextView(this); textView.setText("First Stack Activity "); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getParent(), SecondStackActivity .class); ActivityStack activityStack = (ActivityStack) getParent(); activityStack.push("SecondStackActivity", intent); } }); setContentView(textView); } }
Call next again
public class SecondStackActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textView = new TextView(this); textView.setText("First Stack Activity "); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getParent(), ThirdStackActivity .class); ActivityStack activityStack = (ActivityStack) getParent(); activityStack.push("ThirdStackActivity", intent); } }); setContentView(textView); } }
Works on emulator 2.2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With