Tomap
Tomap

Reputation: 638

Finish activity without waiting for completion

I've been a bit tormented by finding out a way to finish an activity without waiting for it to complete. I know that a simple "return;" statement will do the trick but in my architecture I can't do it.

I currently have:

public abstract class BaseFragmentActivity extends FragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (savedInstanceState != null) {
               finish();
               return;
          }
     }
}

and

public class MainActivity extends BaseFragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          /* code still executed here even when parent finish(); is called */
     }
}

How can I prevent the execution of the code in MainActivity once the finish() in my abstract class is called?

UPDATED CODE:

public abstract class BaseFragmentActivity extends FragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (savedInstanceState != null) {
               finish();
               return;
          }
     }
}

public class MainActivity extends BaseFragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (isFinishing()) {
              return;
          }
     }
}

Strange thing though, the "return" seems to work, but my FragmentManager somehow launches the fragment at the end.

Upvotes: 3

Views: 452

Answers (2)

triggs
triggs

Reputation: 5900

Try wrapping it in

if(isFinishing()){

    //code you don't want to execute if finish() is called

}

Upvotes: 1

Ahmed Ekri
Ahmed Ekri

Reputation: 4651

why don't you create a boolean and set it true and finish will be triggered ?

boolean end = false;

here you make it true

public abstract class BaseFragmentActivity extends FragmentActivity {
     protected void onCreate(Bundle savedInstanceState) {
end = true;
     }
}

and then use if statement

public class MainActivity extends BaseFragmentActivity {
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
if(end = false){
//stuff here}
else {
finish();
}
     }
}

Upvotes: 1

Related Questions