Question

I'm developing an android application which contains a tabhost having 4 tabs. All 4 tabs navigate to same class Activity1 which extends activitygroup.

From Activity1 i'm again navigating to another activity Activity2 using following code,

Intent intent = new Intent(arg1.getContext(), Activity2.class);
            replaceContentView("sample", intent, Intent.FLAG_ACTIVITY_CLEAR_TOP);

public void replaceContentView(String id, Intent newIntent, int flag) {
    View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(flag)).getDecorView(); 
    this.setContentView(view);

Now from this new activity i need to come back to Activity1 when device back button is pressed. I overrided onbackpressed() method in Activity2 with following code,

Intent intent = new Intent(this, Activity1.class); 
    Activity parentActivity = (Activity1)getParent(); 
    parentActivity.replaceContentView("sample", intent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT );

When i run the code, it actually move out of Activity2 but it also move out of Activity1. what i need is to come back to Activity1 from Activity2...can anyone help me?

Was it helpful?

Solution

A simple call to finish() can address your needs.

You can refer Android: Go back to previous activity for detailed description.

OTHER TIPS

Try this code

    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
     {
        this.moveTaskToBack(true);
        return true;
     }
    return super.onKeyDown(keyCode, event);
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top