Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art
HomeF.A.Q.
Community
Writing Recipes
Login
Navigate different activities within a TabView
Contributed by Pratik Rupwal 2011-05-26 01:26:57 (updated 2011-07-21 01:48:14)
In Published Edition? Yes
0
Votes
Problem

You want to move from an activity within a tab view to another activity within the same tab.

Solution

It can be achieved by replacing the content view of the tab by the new activity you want to move to.

Discussion

When an activity (lets call it as 'calling activity' here onwards) within a 'TabView' calls another activity(lets call it as 'called activity' here onwards) through an intent,the 'TabView' gets replaced by the view of called activity. To show the called activity within the 'TabView' we can replace the view of calling activity by the view of called activity so the TabView remains stable.To achieve this the calling activity needs to be extended from 'ActivityGroup' rather than 'Activity'.

Below activity 'Calling' extended from 'ActivityGroup' has been set within a 'TabView'.

//'Calling' activity.

public class Calling extends ActivityGroup implements OnClickListener
{
	Button b1;
	Intent i1;

	/** Called when the activity is first created.*/
    	@Override
    	public void onCreate(Bundle savedInstanceState)
    	{
		super.onCreate(savedInstanceState);
            
        	setContentView(R.layout.calling);
		
		b1=(Button)findViewById(R.id.changeactivity);

		b1.setOnClickListener();
	}

	public void onClick(View view)
	{
		// This creates an intent to call the 'Called' activity 
		
		i1=new Intent(this.getBaseContext(),Called.class);

		// calls the method to replace View.

		replaceContentView("Called", i1);
	}

	// This method is used for replacing the view of 'Calling' activity by 'Called' activity.
	
	public void replaceContentView(String id, Intent newIntent) 
	{
		//Obtain the view of 'Called' activity using its Intent 'newIntent' 
		View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); 

		//set the above view to the content of 'Calling' activity.
		this.setContentView(view);
	}
}

The 'Called activity' can also call another activity (say 'CalledSecond') as below.

//'Called activity'

public class Called extends Activity implements OnClickListener
{
	Button b1;
	Intent i1;
	Calling caller;

	/** Called when the activity is first created.*/
    	@Override
    	public void onCreate(Bundle savedInstanceState)
    	{
		super.onCreate(savedInstanceState);
            
        	setContentView(R.layout.called);
		
		b1=(Button)findViewById(R.id.changeactivity);

		b1.setOnClickListener();
	}

	public void onClick(View view)
	{
		// This creates an intent to call the 'CalledSecond' activity 
		
		i1=new Intent(this.getBaseContext(),CalledSecond.class); /** 'CalledSecond' can be any activity,
                                                           even the 'Calling'(In case backward navigation is required)*/ 

			
		// Initialize the object of the 'Calling' class.
		caller=(Calling)getParent();

		// calls the method to replace View.

		caller.replaceContentView("CalledSecond", i1);
	}
}
Comments (0)
Leave a comment
Edit History (7)
There are no (moderator-approved) comments on this recipe yet.