0

I am using TabActivity(for example, TActivity) to create tab and in every tab I am loading instance of same activity(for example, MainActivity). Through this activity I want to switch another activitity(for example, ChildActivity which will be same for every tab i.e. in every tab we will get instance of this activity) using Button click. This button is hardcoded in original TabActivity(TActivity). Now I am able to do up to this. But Now I want to switch between 'MainActivity' and 'ChildActivity' using same Button.

MainActivity:

class MainActivity ...{

  onCreate(){
        Btn.setOnClickListener(new OnClickListener(--) {
            public void onClick(--){
                    Intent intent = new Intent(getBaseContext(),ChildActivity.class);
                replaceContentView("MainActivity", intent);
  }});
}//MainActivity

public void replaceContentView(String id, Intent newIntent) {
        View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)) .getDecorView(); this.setContentView(view);
        }//replaceContentView

in ChildActivity:

public void hideCurrentActivity(){
Intent intent = new Intent(getBaseContext(),MainActivity.class);
        replaceContentView("ChildActivity", intent);
}
public void replaceContentView(String id, Intent newIntent) {
        View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) .getDecorView(); this.setContentView(view);
        }

Ok, now problem is that this code creates fresh activity everytime. So all previous data is getting lost once I switch to previous activity.But i don't want to create fresh activity instead just want to bring old one to front.

Can anybody help? Thanks in advance.

userx
  • 806
  • 1
  • 11
  • 23
  • You can use `finish()` in your child activity to go back to main activity instead of providing an `Intent`. – Naddy Dec 12 '13 at 02:45
  • finish() is closing whole app. – userx Dec 12 '13 at 02:50
  • `ChildActivity.this.finish();` – Naddy Dec 12 '13 at 03:00
  • The reason of this may be I am using interface to call 'MainActivity' from 'TActivity' and to call 'ChildActivity' from 'MainActivity'. Without interface I didn't get any way to do this things. So when call finish(), it closes whole app. – userx Dec 12 '13 at 03:02
  • ChildActivity.this.finish() also not working – userx Dec 12 '13 at 03:07
  • Store your data in a `SharedPreference`. – Naddy Dec 13 '13 at 03:44
  • its instance data, there r many instances, so don't want to share in sharedPreferences. Also i have override 'backPressed' button to kill some non-ui threads. – userx Dec 13 '13 at 08:42
  • @Naddy thax for taking interest. Temporary my problem is solved. Actually now I am passing values through intent every time, and fetching those value at starting. So though activity started freshly, I can get all variables previous values. – userx Dec 24 '13 at 06:00

0 Answers0