Resume to last activity even when the app is forced to close by the user

0

I have an application similar to taxi apps. It turns out that in the driver application while it is moving to fetch the passenger, if he forcibly closes the application or the device restarts or crashes, when he opens the app again he returns to the Home screen. I've already implemented onSaveInstance() and it works perfectly for when the app is killed by the system. But I need a solution for when the user wants to terminate the app or when the device restarts it goes back to the last% running% that was the follow up of the race.

    
asked by anonymous 22.08.2016 / 05:26

1 answer

0

I had to do something like this once ...

I created a class that inherits from application and created, within it these methods to read and write the current activity.

public class AppApplication extends Application
{
    private Activity mCurrentActivity = null;
    public Activity getCurrentActivity(){
        return mCurrentActivity;
    }
    public void setCurrentActivity(Activity mCurrentActivity){
        this.mCurrentActivity = mCurrentActivity;
    }
}

I also created a class that contained the base implementation of the activity's

public class BaseActivity extends Activity{

    protected AppApplication application;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        //faz o cast para o tipo da minha classe de Application
        application = (AppApplication)this.getApplicationContext(); 
    }

    @Override
    protected void onResume() {
        super.onResume();
        //cada vez que é criada ou é resumida a activity grava a activity atual
        //ou seja sempre vou ter a Activity atual "gravada"
        //você pode fazer guardar ao invés da instância da activity, grava uma TAG que identifica e grava em algum local, e na hora de abrir o app você ler deste local.
        application.setCurrentActivity(this); 
    }
}

I've changed all my activity to

public class Exemplo extends BaseActivity {
    ...
}

and in the manifest add your AppApplication class

<application
            android:name=".Globais.AppAplication"
            android:icon=...
            android:label=...
            android:theme=...>
        <activity...

I know it's not very well what you need, but I decided to share my experience, because when you do not have anything .. something is very useful ..

I hope I have helped !!

    
22.08.2016 / 13:46