open last activity when the app is killed

2019-09-20 12:20发布

问题:

then in my app there are 103 activity, in sequence SplashActivity -> Menu ---> Q_001 ---> Q_002 ... (...) ..... Q_finish. in each of the activity I have a button to put exit from the app (like a home button), and a button that returns to the Menu. Now I want that if the user kills the app, and open it at another time returns to the activity that was using,

I know I have to try to change the SpalshActivity The last activity you were doing, if you were not carrying out any activity to continue His Work this is the code of SplashActivity:

package org.somename;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle TravisIsAwesome) {
        super.onCreate(TravisIsAwesome);
        setContentView(R.layout.splash);



        Thread logoTimer = new Thread (){


            @Override
            public void run(){
                try{
                    sleep(500);
                    Intent menuIntent = new Intent("org.somename.MENU");
                    startActivity(menuIntent);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                finally{
                    finish();

                }
            }
    };

    logoTimer.start();


    }






    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

keep saying that I do not care about saving activity menu, but all the activities starting from "Q_001" and ending activity "Q_finish." I tried to use new this method: I have suggested in the previous question and I have implemented this code:

package org.somename;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle TravisIsAwesome) {
        super.onCreate(TravisIsAwesome);
        setContentView(R.layout.splash);

         int last_activity = getLastActivityIdFromSharedPreferences();
            if (last_activity == 1)
            {
                this.startActivity(new Intent(this, Q_001.class));
                finish();
            }

            int last_activity2 = getLastActivityIdFromSharedPreferences2();
            if (last_activity2 == 2)
            {
                this.startActivity(new Intent(this, Q_002.class));
                finish();
            }

        Thread logoTimer = new Thread (){


            @Override
            public void run(){
                try{
                    sleep(500);
                    Intent menuIntent = new Intent("org.somename.MENU");
                    startActivity(menuIntent);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                finally{
                    finish();

                }
            }
    };

    logoTimer.start();


    }






    private int getLastActivityIdFromSharedPreferences2() {
        // TODO Auto-generated method stub
        return 2;
    }






    private int getLastActivityIdFromSharedPreferences() {
        // TODO Auto-generated method stub
        return 1;
    }






    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

but it is not part of the SplahActivity, and does not return to the Activity executed . goes directly to the menu, with a SplahActivity of blank page

Thank in advance

回答1:

I agree with @2Dee.

First thoughts were that your are not really learning the current Android development, I think. I suppose most people would go for one activity for splash (if you really need one) and a second activity for holding the 100 buttons. This 100 buttons would be hold inside a viewpager. It is a bit more complex to understand at first but it is the way to go, in my opinion. The view pager will recycle, is efficient and a powerful tool to leverage.

But maybe you are doing a school exercise ?

Edit: I see 2 ways, probably someone can find better.

  1. You can save in memory where the user is. When the user click to exit, just save in a SharedPreferences the current question and retrieve it at start : sharedPreferences
  2. You can make that clicking exit button jsut call onBackPressed() and maybe override onBackPressed. If you do that the activity will be send to background and the user to Home. If he goes back to it, it will restart on that last activity. You must still save it in case the user kill the app or Android kills the app for ressource management.

Edit 2 :

In Splash Activity, you retrieve an integer. in the main activity (the one holding the question and the buttons) you write the question number.

That gave something like that :

splash activity :

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

   // Restore preferences
   SharedPreferences settings = getSharedPreferences(LAST_QUESTION, 0);
   int number = settings.getInt("last_question", 1); // default value is to start again
   goToQuestionNumber(number); // you better call this in onResume(). 
}

In the exiting method launched by the onClik()

 SharedPreferences settings = getSharedPreferences(LAST_QUESTION, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putInt("last_question", currentQuestionNumberThatYouShouldKnow);


回答2:

Ok, as I thought, I believe you need to rethink your app's architecture :)

Make one Activity and one Fragment. The Activity will be responsible for switching the Fragment in view and creating them. It will also hold a reference to the urls necessary for the webviews.

This is the way I would do it (but it might take a complete refactoring of your code - which is not a bad exercise if you think about it) :

in Activity :

// declare your urls in the res/values/strings.xml and use them here.
private int[] webViewsUrls = {R.string.url1, R.string.url2, ...}; 

in Activity's onCreate :

SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
int activityIndex = prefs.getInt("lastActivityIndex", 0);
QuestionFragment fragment = new QuestionFragment();
Bundle bundle = new Bundle();
bundle.putInt("INDEX", activityIndex);
bundle.putString("WEBVIEW_URL", webViewsUrls[activityIndex]);
fragment.setArguments(bundle);
// Show fragment

To add a new Fragment to a ViewGroup inside your Activity layout (if you don't use a ViewPager but rather use a button to change the question in view) :

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
// create you fragment using the method I give above
fragmentTransaction.add(R.id.fragment_container_in_activity_layout, fragment, STRING_FRAGMENT_TAG);
fragmentTransaction.commit();

To replace the fragment already in view, use the replace method instead of add.

The on click on a button, or in a ViewPager, change the Fragment in view and do not forget to save the new value for activityIndex in SharedPreferences when starting your Fragment.

I believe the suggestion of @Poutrathor is quite good, you could refer to this answer I gave yesterday if you want to use ViewPager, which I think would fit your requirements better than having 100+ Activities.