Android: to start new Intent from ListView in the same FrameLayout, hosted in TabHost

Solving this issue took me several days of investigation and research.
The structure of my app is the following:



    
         
        
            
    

Three tabs are set for this TabHost.
Each tab click opens proper ListView page.
OnClick action for ListView item opens proper Activity, which was done, by the following code:

Intent intent = new Intent(this, newActivity.class);
this.startActivity(intent);

The problem was, that newActivity was opened in new Window, but I wanted to open it in the same window.
Asking for help on stackoverflow.com didn’t help.
And then suddenly I found the following solution Experience – Multiple Android Activities in a TabActivity
Now my code to run newActivity look as following:

TabGroupActivity parentActivity = (TabGroupActivity)getParent();
            parentActivity.onBackPressed();
Intent newIntent = new Intent(getParent(), newActivity.class);
parentActivity.startChildActivity("newActivity", newIntent);

Note: if you have any kind of Widget functionality, which pop-ups in your View, then make sure, that getParent() is used for Context.

2 comments

  1. hello, i need your help

    as you said
    OnClick action for ListView item opens proper Activity, which was done, by the following code:
    1Intent intent = new Intent(this, newActivity.class);
    2this.startActivity(intent);

    i want the same thing in my app and i have been searching around for like 40 days , on stackoverflow, xda and all , what i want actually is it should be a simle listview displaying some items like apple,mango,orange
    and when the user clicks an item, he will be taken to some other activity

    so, the question is How do i implement onlistitem click listener ?
    please help! , im stuck
    please give me an example code
    waiting for your reply…

  2. Hi,

    take a look on my code for SimpleList implementation.

    public class App extends ListActivity {
    	static final ArrayList> list =
        		new ArrayList>();
    	public Dialog dialog;
    	public String about_text;
    	public TextView text;
    	
    	private void populateList() {
        	list.clear();
        			
            HashMap temp = new HashMap();
            temp.put("name","First line");
            list.add(temp);
            HashMap temp1 = new HashMap();
            temp1.put("name","Second line");
            list.add(temp1);
            HashMap temp2 = new HashMap();
            temp2.put("name","Third line");
            list.add(temp2);
            HashMap temp3 = new HashMap();
            temp3.put("name","Forth line");
            list.add(temp3);
            
        }
    	
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		
    	    SimpleAdapter adapter = new SimpleAdapter(
    	    	    this,
    	    	    list,
    	    	    R.layout.about_list,
    	    	    new String[] {"name"},
    	    	    new int[] {R.id.option_name}
    	    	    );
    	    	    populateList();
    	    	    getListView().setBackgroundColor(Color.parseColor("#FFFFFF"));
    	    	    getListView().setCacheColorHint(Color.parseColor("#FFFFFF"));
    	    	    setListAdapter(adapter);
    	    	   
    	    	    dialog = new Dialog(AboutApp.this);
    		    	
    		    	dialog.setContentView(R.layout.about);
    	    	    
    	}
    	
    	@Override
    	protected void onListItemClick(ListView l, final View v, int position, long id) {
    		super.onListItemClick(l, v, position, id);
    		
    		Intent newIntent = new Intent(getParent(), newActivity.class);
    
    		switch ((int)l.getItemIdAtPosition(position)) {
    	    case 0:
    	    	// here is the code for first line
               
                startActivity(newIntent);
    	    	break;
    	    case 1:
    	    	// here is the code for second line
    
    	    	break;
    	    case 2:
    	    	// here is the code for third line
    	    	
    	    	break;	
    	    case 3:
    	    	// here is the code for forth line
    	    	
    	    	break;
    	    	
    //	    default:
    //	        return super.onOptionsItemSelected(item);
    	    }
    
    	}
    }
    

Leave a Reply

%d bloggers like this: