0

I have a base class that checks if app is launched from history, and no processes of app are available, then redirect user to MainActivity.

This is my base class code;

    public abstract class BaseActivity extends AppCompatActivity {


    protected void onCreate(Bundle savedInstanceState) {
       if(MainActivity.instance == null){
       //instance is a static variable in MainActivity

             Intent i= new Intent(this, MainActivity.class);
             startActivity(i);
             this.finish();
             return;   
       }else{

           setContentView(getLayoutRes());

       }
    }   

   //Child app override this method, and provides their layout.
   protected abstract int getLayoutRes();

Child Activity

public class MapActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //1000 line of code 

}   

@Override
protected int getLayoutRes() {
    return R.layout.activity_map;
}

Now what it does when user launches the app from history, and no processes of the app are available, then it reads the check of BaseActivity and tries to start the MainActivity intent, and does not set the layout,

but the problem is that it does not stop there, it goes to MapActivity - onCreate() method and start executing the other lines of code, as i have not set layout, it crashes while trying to read findViewById(), it also fails at other static variables.

So the whole point of redirecting user to MainActivity when the process is killed is not fullfiled.

I need to know how to stop executing code of child class when BaseClass start the intent

dev90
  • 7,187
  • 15
  • 80
  • 153
  • add return; in onCreate() of mainActivity. – Hemant Parmar Jan 16 '18 at 09:18
  • i want to launch or redirect user to mainActivity when user launch any other activity form history – dev90 Jan 16 '18 at 09:22
  • sorry i am not getting your point. history means what if user go from 1 activity to 9 they all are in history now on activity 10 which have already in history than must redirect to MainActivity ? am i right ? – Hemant Parmar Jan 16 '18 at 09:26
  • Don't use static fields to store information. You can use the instance state for that. – Henry Jan 16 '18 at 09:36
  • let say user is on 9th activity, he presses Home button, now he opens `facebook app`, then `twitter app`, and after a while he presses history button, and opens my app from there, by this time, android has removed all the process of my app, it will try to launch the 9th activity, but 9th activity has some static references, and it requires MainActivity to be there, so baseClass checks that if MainActivity static references are null then just redirect user to MainActivity – dev90 Jan 16 '18 at 09:37
  • @Henry; I understand your point, but here we need to have a static class that have methods, all activities need to use. – dev90 Jan 16 '18 at 09:44
  • @Kirmani88 but it simply does not work. Find another way to structure your code. – Henry Jan 16 '18 at 09:47
  • @Henry can't we stop the child class to not execute its `onCreate()` if parent class launches an intent – dev90 Jan 16 '18 at 09:54
  • 1
    Why don't you let the parent class have a field `protected boolean didSetContentView` which is assigned `false` if the parent `Activity` launches MainActivity and `true` if the parent `Activity` calls `setContentView(getLayoutRes());` ? Then the child class (MapActivity) can return from `onCreate()` if `didSetContentView` is `false`. – Bö macht Blau Jan 16 '18 at 20:02

1 Answers1

0

In the MapActivity, you can check the value returned by isFinishing() just after super.onCreate(). If it's true then return from MapActivity's onCreate() otherwise proceed.

Rishabh Jain
  • 297
  • 2
  • 13