Payday Loan Payday Loan

Hi,
I want to show you something that tooked me some time to figure out.
When you use TabActivity, TabHost, or TabWidget which resumes to TABS, you will need also to open new Intents from this TabActivity, and to come back to it, whitout closing it. So you will notice that if you want to open a dialog, a force close error will appear. This will appear if you want to open a Intent from a listview that is in a tab.
So our, example will treat this. We have a listview in a tabcontent, and when we click on a item, a new Activity
will be open.
This strategy uses Nested Activities or ActivityGroup class.
First i will show you Main.java class that contains the tabs:

import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
 
public class Main extends TabActivity{
 
	public TabHost tabHost;
 
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);
 
            // Get the tabHost
	    this.tabHost = getTabHost();
 
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab
 
	    // Create an Intent to launch the first Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, ListViewGroup.class);
 
	    // Initialize a TabSpec for the first tab and add it to the TabHost
	    spec = tabHost.newTabSpec("ListViewGroup").setIndicator("ListViewGroup",
	    		getResources().getDrawable
	    				(null)) // Replace null with R.drawable.your_icon to set tab icon
	    				.setContent(intent);
	    tabHost.addTab(spec);
 
            // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, SecondActivityGroup.class);
 
	    // Initialize a TabSpec for the second tab and add it to the TabHost
	    spec = tabHost.newTabSpec("SecondGroup").setIndicator("SecondGroup",
	    		getResources().getDrawable
	    				(null)) // Replace null with R.drawable.your_icon to set tab icon
	    				.setContent(intent);
	    tabHost.addTab(spec);
 
	    tabHost.setCurrentTab(0);
	}

Now, you need to create class ListViewGroup.java. I will create only this class for demonstration purposes only. If you want to work this program you need to create SecondActivityGroup.java also. You can put same content as ListViewGroup.java if you do not know to put anything else.

import java.util.ArrayList;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
 
public class ListViewGroup extends ActivityGroup {
 
        // Keep this in a static variable to make it accessible for all the nesten activities,
 //lets them manipulate the view
	public static ListViewGroup group;
 
        // Need to keep track of the history if you want the back-button to
// work properly, don't use this if your activities requires a lot of memory.
	private ArrayList<View> history;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	      super.onCreate(savedInstanceState);
	      this.history = new ArrayList<View>();
	      group = this;
 
              // Start the root activity withing the group and get its view
	      View view = getLocalActivityManager().startActivity("ListView", new
	    	      							Intent(this,ListView.class)
	    	      							.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
	    	                                .getDecorView();
 
              // Replace the view of this ActivityGroup
	      replaceView(view);
 
	   }
 
	public void replaceView(View v) {
                // Adds the old one to history
		history.add(v);
                // Changes this Groups View to the new View.
		setContentView(v);
	}
 
	public void back() {
		if(history.size() > 0) {
			history.remove(history.size()-1);
			setContentView(history.get(history.size()-1));
		}else {
			finish();
		}
	}
 
   @Override
    public void onBackPressed() {
    	ListViewGroup.group.back();
        return;
    }
 
}

After you setted this class, now it appears a new class there, that is called ListView.java.
Let’s create it.

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
 
public class ListView extends ListActivity{
 
       // Data to put in the ListAdapter
       private String[] cities = new String[] {"Rome", "Budapest", "Washington"}
    	private ListAdapter adapter;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        fillData();
    }
 
    // Fill the list with some data, this can be anything really
    public void fillData() {
    	ArrayAdapter citiesAdapter = new ArrayAdapter(this, R.layout.city_row, cities);
        setListAdapter(citiesAdapter);
    }
 
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
 
        Intent i = new Intent(this, ShowCity.class);
        String city_name = (String) getListAdapter().getItem(position);
        i.putExtra("city_name", city_name);
 
        // Create the view using FirstGroup's LocalActivityManager
        View view = FirstGroup.group.getLocalActivityManager()
        .startActivity("show_city", i
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
        .getDecorView();
 
        // Again, replace the view
        FirstGroup.group.replaceView(view);
    }
}

As you saw, we have implemented a listView with 3 cities. On Clicking a city will open a new intent, a new Activity called ShowCity.java.Now, Show city can contain anything you want, but our purpose was fullfilled.
This last Activity will open without problems.
Layouts are defaults for TabActivity and ListView. So you will find them on Android Developer page.

||||| 4 I Like It! |||||

3 Responses so far.

  1. Piraba says:

    My question is switching between tab using activity group it want to display last activity. I want to show last open/visited screen when we navigate the tab.My one is go to first screen

    probably my ActivityGroups are being created again and again when you switch between tabs So groups want to create only once and resumed when i switch between tabs

    Please tell me the solution for this…

    Thanks in advance

  2. Piraba says:

    Same code I did. When we navigate the tab it didn’t go to last opened activity.

    I have confused on ListViewGroup class.Here onCreate method you are calling

    View view = getLocalActivityManager().startActivity(“ListView”, new
    Intent(this,ListView.class)
    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
    .getDecorView();

    // Replace the view of this ActivityGroup
    replaceView(view);

    Then when tab navigation time it will go to 1st activity.That mean ListView.class.

    Please help me

  3. elior says:

    I’ve search for this solution for few hours !! thanks!


Search

Popular

Sponsors