Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

android - open last activity when the app is killed

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...