Question

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.

Était-ce utile?

La solution

Try wrapping it in

if(isFinishing()){

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

}

Autres conseils

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();
}
     }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top