简体   繁体   中英

Activity gets killed when orientation is changed

I have two activities in my project- Splash and AActivity. Splash is the main activity and is working fine. But if i change the orientation while Splash activity is running, the UI of splash activity goes off but it opens the AActivity after 10 sec.

code for splash activity is -

public class Splash extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Thread timer = new Thread(){
            public void run() {
                try{
                    sleep(10000);   

                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent AActivityIntent = new Intent("com.example.ex.AACTIVITY");
                    startActivity(AActivityIntent);
                }
            }};

        timer.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();                                   
    }

}

I want to retain the UI of splash activity for 10 seconds even if orientation is changed. After 10 sec splash activity should be finished. How to do it ???

I suggest you doing the following:

public class Splash extends Activity {
    private Thread timer;
    private volatile long timeLeft;
    private long timeStarted;
    private long timeStopped;

    private static final long TIME_TO_SHOW = 100000
    private static final String KEY_TIME_LEFT = "timeLeftToRun";

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

        if (savedInstanceState != null) {
            timeLeft = savedInstanceState.getLong(KEY_TIME_LEFT, 0);
        } else {
            timeleft = TIME_TO_SHOW;
        }

        timer = new Thread() {
            public void run() {
                try {
                    sleep(timeLeft);   
                    Intent AActivityIntent = new Intent("com.example.ex.LISTSCREEN");
                    startActivity(AActivityIntent);
                } catch(InterruptedException e) {}
            }
        }};
        timeStarted = SystemClock.elapsedRealtime();
        timer.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        timer.interrupt();
        timeStopped = SystemClock.elapsedRealtime();
        finish();                                   
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        timeLeft -= timeStopped - timeStarted;
        if (timeLeft > 0) outState.putLong(timeLeft);
    }
}

The main idea here is that you kill the thread if the activity is killed, but you take a note for how long it has run and how much time it has left. When the activity is restored, you do the same actions, except you have to wait for a smaller amount of time.

The code above is, of course, untested, but it should illustrate the idea.

Insert into your manifest these below block. It means orientation change situation controlled by your "activity". http://developer.android.com/guide/topics/manifest/activity-element.html#config

android:configChanges="orientation"

And more.

Override "onConfigurationChanged" method.

Try this. You can do everything you want.

使用Alarmmanager而不是使用Thread延迟启动活动,即使您退出该应用程序,也可以随时取消待处理的Alarm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM