title: How do I get callback on pressing device's back button?
body: <p>According to my requirements i need to implements tabs for my apps. Such that each tabs associated with multiple activities. Like: Tab1, Tab2, Tab3...etc.
  Tab1: Activity1,Activity2,Activity3....etc. I want to launch activities in such order:
  eg: Tab1 -> Activity1 -> Activity2. This is fine i have doen it.</p>
  
  <p>But if i press device back key from Activity2 then it is coming to home screen but it should come activity1. But if i am going back through ui component(like back button) then it is working fine.</p>
  
  <p>Below are my TabGoupActivity and Activity 1, Activity2 implementation.</p>
  
  <p>TabGruopActivity:-</p>
  
  <pre><code>public class TabGroupActivity extends ActivityGroup {
  
      private ArrayList&lt;String&gt; mIdList;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);       
          if (mIdList == null) mIdList = new ArrayList&lt;String&gt;();
      }
  
      @Override
      public void finishFromChild(Activity child) {
        LocalActivityManager manager = getLocalActivityManager();
        int index = mIdList.size()-1;
  
        if (index &lt; 1) {
            finish();
            return;
        }
  
       // manager.destroyActivity(mIdList.get(index), true);
        mIdList.remove(index); index--;
        String lastId = mIdList.get(index);
        Intent lastIntent = manager.getActivity(lastId).getIntent();
        Window newWindow = manager.startActivity(lastId, lastIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        setContentView(newWindow.getDecorView());
        }
  
        public void startChildActivity(String Id, Intent intent) {    
        Window window = null;
        try{
         window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) );
        }catch(Exception e){
            System.out.println(e.toString());
        }
        if (window != null) {
            mIdList.add(Id);
            setContentView(window.getDecorView()); 
        }
      }
  
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
            return true;
        }
        return super.onKeyDown(keyCode, event);
      }
  
      @Override
      public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        }
        return super.onKeyUp(keyCode, event);
      }
  
      public void  onBackPressed  () {
        int length = mIdList.size();
        if ( length &gt;= 1) {
            Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
            current.finish();
        }  
      }
  }
  </code></pre>
  
  <p>TabGroup1Activity: Which extends TabGroupActivity(Above Activity):-</p>
  
  <pre><code>public class TabGroup2Activity extends TabGroupActivity{
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          startChildActivity("Activity2", new Intent(this, DemoActivity2.class));
      }
  
  }
  </code></pre>
  
  <p>Activity1:-</p>
  
  <pre><code>public class DemoActivity2 extends Activity {
  
      Context context;
  
      protected static LocalActivityManager mLocalActivityManager;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.demo_activity2);
          context = this;
  
          Button button = (Button) findViewById(R.id.Button01);
          button.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  Intent intent = new Intent(context, DemoActivity3.class);
                  TabGroupActivity parentActivity = (TabGroupActivity)getParent();
                  parentActivity.startChildActivity("Activity3", intent);
              }
          });
  
          Button button2 = (Button) findViewById(R.id.Button02);
          button2.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  Intent intent = new Intent(
                          ScrollableTabActivity.ACTION_CHANGE_TAB);
                  intent.putExtra(ScrollableTabActivity.CURRENT_TAB_INDEX, 0);
                  sendBroadcast(intent);
              }
          });
      }
  
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_BACK) {
              //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
              return true;
          }
          return super.onKeyDown(keyCode, event);
      }
  
      @Override
      public boolean onKeyUp(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_BACK) {
              TabGroupActivity parentActivity = (TabGroupActivity)getParent();
              parentActivity.onBackPressed();
              return true;
          }
          return super.onKeyUp(keyCode, event);
      }
  }
  </code></pre>
  
  <p>Activity2:-</p>
  
  <pre><code>public class DemoActivity3 extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.demo_activity3);
  
          Button button = (Button) findViewById(R.id.Button01);
          button.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  TabGroupActivity parentActivity = (TabGroupActivity)getParent();
                  parentActivity.onBackPressed();
              }
          });
      }
  
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_BACK) {
              //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
              return true;
          }
          return super.onKeyDown(keyCode, event);
      }
  
      @Override
      public boolean onKeyUp(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_BACK) {
              TabGroupActivity parentActivity = (TabGroupActivity)getParent();
              parentActivity.onBackPressed();
              return true;
          }
          return super.onKeyUp(keyCode, event);
      }
  }
  </code></pre>
  
  <p>Can any one tell to me why device back button is not working?</p>