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

Asked

Viewed 106 times

0

I have an app similar to taxi apps. It happens that in the driver’s app while he is moving to pick up the passenger, if he forcibly closes the app or the device restarts or gives some crash, when he opens the app again he returns to the Home screen. I’ve already implemented the onSaveInstance() and works perfectly for when the app is killed by the system. But I need a solution for when the user wants to quit the app or when the device restarts it goes back to the last activity running that was to follow the race.

  • You can save in sharedPrefferences the last screen of the user, implement a method and call it always in onDestroy(); If the user closes the app.. you can create a service that forces him to stay with the app open, but I think it’s kind of "dangerous".

  • @Carlosbridi, I think calling onDestroy is not a good idea... I would record onResume, so I understood, for example if the user takes the battery, will not be able to call onDestroy for example..

  • @Carlosbridi I tried to use sharedprefferences, but when I forcibly close sharedprefferences is clean and back to default. It seems some android bug I’ve even researched about...

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 record 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 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 changed all my activitys 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 quite what you need, but I decided to share my experience, because when you don’t have anything. Something is very useful..

I hope I’ve helped!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.