3

This code is for my splash screen Activity:

    @Override
    protected void onResume() {
    super.onResume();
    handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
        }
    }, 5000);
}

When I click the device back button ,MainActivity become finish and again SplashActivity comes to up,and again it's onResume method get call, problem is here that handler doesn't execute again and MainActivity doesn't start again after 5 second ! I want it execute again!

what's the problem and what should i do?

thanks for attention.

MehDi
  • 504
  • 6
  • 20

5 Answers5

1

I modified your code a little bit check this

@Override
    protected void onResume() {
    super.onResume();
   new  android.os.Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
        }
    }, 5000);
}
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
  • Have you done something in your MainActivity.class in onStop() or onDestroy() method – Rahul Khurana Sep 28 '16 at 04:52
  • I totally wanna to force the program start again from the first point when it goes to onStop. – MehDi Sep 28 '16 at 04:54
  • @RahulKhurana What is the different between your answer and OP question – Piyush Sep 28 '16 at 04:54
  • yes that is the problem. Because onResume() of previous activity is called after onPause() of current activity. and after that onStop() or onDestroy() method got called. So that method is getting called but you have applied finish on it. So its not openning new activity. Do this in onPause() method in your MainActivity.class – Rahul Khurana Sep 28 '16 at 04:54
  • oh yes thanks for your reminding, I call it in onPause() but still doesn't work :) – MehDi Sep 28 '16 at 05:00
  • why you need to call finish() method by your own. if the activity is already finishing – Rahul Khurana Sep 28 '16 at 05:01
  • @RahulKhurana it doesn't finish itself , it only goes into onStop() and when user again open it,it goes to onResume() (instead of starting from the first). I don't want that , this program is a game , think that situation when user's phone ringing (and my game become stop) , after he answer the call,my game start form the first (and not to continue previous game state) sorry for bad English :D – MehDi Sep 28 '16 at 05:33
1

Move your code snippet to start the MainActivity to onCreate(). Call finish() immediately after startActivity() method. Also remove finish() call in MainActivity class.

Piyush
  • 18,895
  • 5
  • 32
  • 63
Febi M Felix
  • 2,799
  • 1
  • 10
  • 13
  • when I do it ,my Splash Activity become finish! I don't want that,I generally wanna to force the program start again from the first point when user click device back button and it goes to onStop – MehDi Sep 28 '16 at 05:09
1

Put you code in MainActivitys onCreate() Like this :

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            handler = new Handler();
            handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                }
            }, 5000);
     }

Putting handler like this in onResume() is not a good approach

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
1

First of all I would like to tell you that this is not the right way to add splash screen. There is no need of a handler at all.

In general splash screens are just that natural delay that you see when app launches. Adding your own delay of 5 seconds is Not Recommended At All.

Now if you want to stylize that natural delay and make a proper custom splash screen then you need to follow this procedure.

Start by creating a background_splash.xml drawable that will serve as the ui of splash screen

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/gray"/> <item> <bitmap android:gravity="center" android:src="@mipmap/ic_launcher"/> </item> </layer-list>

Next you have to set this as your Spash screen background in style.xml

<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> </style> <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowBackground">@drawable/background_splash</item> </style> </resources>

Now you configure your android manifest accordingly

<activity android:name=".SplashActivity" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

Finally code your Splash screen java class like this

public class SplashActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); } }

Source: bignerdranch

So, you are how you didn't have to manipulate the application launch time with handler thread. This will eradicate the white background on app launch delay and give you customised xml drawable.

For reference you can check how all Google apps like YouTube etc launch without a white background delay and other high quality apps launch.

Please consider using this correct flow for your Splash screen. It's just a window background nothing complicated like multithreading, handler etc

Nishant Dubey
  • 2,802
  • 1
  • 13
  • 18
  • thank you very much for this:) but my question isn't how make splash screen,I wanna different approach, I generally wanna to force the program start again from the first point when it goes to onStop – MehDi Sep 28 '16 at 05:21
  • You are asking to alter basic android activity life cycle it's anti ux pattern. Did some client ask you to do it ? Or is it just an experiment. Because I personally would never recommended you doing it. That just alters the very basics. – Nishant Dubey Sep 28 '16 at 05:31
  • Anyway :) in case this helps you with *just splash screen then do consider alteast upvoting it. Unfortunately I wouldn't be very useful with anti pattern android coding so won't be much useful with your actual aim. – Nishant Dubey Sep 28 '16 at 05:34
  • This program is a game , think that situation when user's phone ringing (and my game become stop) , after he answer the call,my game should start form the first (and not to continue previous game state) sorry for bad English :) – MehDi Sep 28 '16 at 05:35
  • Oh for that you just need to save instance state! Do you know to save instance state ? – Nishant Dubey Sep 28 '16 at 05:39
  • yes I knew it, but no ,not relative to saving state ,process should run from the first , means again show splash screen and again start current level ,like other games. But maybe I should try some other tricks instead of regular ways to do what i need. (and still sorry for bad English :D ) – MehDi Sep 28 '16 at 14:43
1

I was tested your code, It's working for me without any problem. please check my code.

Your really want to execute handler when you press the back button of second Activity. you should put the Handler in the "onStart" or "onResume" only. Because "onCreate" method of First Activity not called when you press the back button of the Second Activity.

If you are scenario is Splash Screen you should close the splash after Intent execute. Because we should show the splash screen only first time only.

@Override
    protected void onResume() {
        Log.i(TAG,"onResume Called");
        super.onResume();
        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        }, 5000);

    }

and SecondActivity code as follows.

protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
   }
Kona Suresh
  • 1,836
  • 1
  • 15
  • 25
  • no i don't wanna only make an splash ,This program is a game , think that situation when user's phone ringing (and my game become stop) , after he answer the call,my game should start form the first (and not to continue previous game state). this code doesn't execute handler again to do what i want. sorry for bad English – MehDi Sep 28 '16 at 14:47