Hossam Oukli
Hossam Oukli

Reputation: 1296

Cannot finish a child tab activity before it starts

I have a TabActivity that is my MainActivity, it has two tabs that are tab1 and tab2.

By default MainActivity starts with the tab1 In front.

When i click on the tab2, the tab2 must start an intent back into the MainActivity after that the tab2 activity must finish, all of that should happen before the setContent(R.layout.tab2) is executed.

The application behavior I'm trying to get is, every time that i click on tab2, the application should back to tab1.

This works fine, but the problem is, the content of tab2 appears for few seconds before the tab2 activity is finished and the intent is started into MainActivity.

I know about the setCurrentTab method, but it's not what I'm Looking for.

Here is my tab2 Activity:

public class tab2 extends Activity
{   
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    if(true)

        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab2);
}
}

Upvotes: 0

Views: 104

Answers (1)

Shobhit Puri
Shobhit Puri

Reputation: 26007

Remove setContentView(R.layout.tab2); . i.e if you don't set any view then no view would be shown. I haven't tried it but it seems that it would work.

Update

As you said in comments that If sometime condition can be false then have it inside if condition:

super.onCreate(savedInstanceState);
if(conditions_is_true){
    Intent intent = new Intent(this,MainActivity.class);
    startActivity(intent);
    finish();
}else{
    setContentView(R.layout.tab2);
}

Upvotes: 1

Related Questions