简体   繁体   English

恢复应用程序时启动画面活动被忽略-Android

[英]Splash screen activity ignored when resuming application - Android

I am wondering why my application ignores my SplashScreen.java activity when resuming the aplication. 我想知道为什么我的应用程序在继续应用时会忽略SplashScreen.java活动。 If I close it with the "Back" button, the splash screen comes up on start, but if I exit with the home button the SplashScreen activity is not being called...:( 如果使用“后退”按钮将其关闭,则启动时会显示初始屏幕,但是如果使用主屏幕按钮退出,则不会调用SplashScreen活动... :(

I even added the onResume event, but the splash screen still wont come up when resuming my app. 我什至还添加了onResume事件,但是在恢复我的应用程序时仍然不会出现启动屏幕。 Thanks!! 谢谢!!

SplashScreen.java SplashScreen.java

public class Splash extends Activity {

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

    }

    protected void onResume(){
        super.onResume();
        spashStart();
        }   


    private void spashStart() {
        Thread splashTimer = new Thread() {
            public void run(){
                try{
                    sleep(5000);
                    Intent mainActivity = new Intent("com.exploreca.tourfinder.MainActivity");
                    startActivity(mainActivity);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                  finish();
                }
            }
        };
        splashTimer.start();
    }

}

Maifest: Maifest:

...

<uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/scena_logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" > 
        <activity
            android:name="com.exploreca.tourfinder.Splash"
            android:label="@string/app_name" >            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>            
        </activity>    
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >            
            <intent-filter>
                <action android:name="com.exploreca.tourfinder.MainActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>            
        </activity>
        <activity 
            android:name=".SettingsActivity"
            android:parentActivityName="com.exploreca.tourfinder.MainActivity">            
        </activity>
        <activity 
            android:name=".TourDetailActivity"
            android:parentActivityName="com.exploreca.tourfinder.MainActivity">            
        </activity>  
        <activity 
            android:name=".NotificationDetails"
            android:label="@string/title_activity_notifDetails_title"
            android:parentActivityName="com.exploreca.tourfinder.MainActivity">            
        </activity>  
        <activity 
            android:name=".SavedEvents"
            android:label="@string/title_activity_SavedEvents" 
            android:parentActivityName="com.exploreca.tourfinder.MainActivity">            
        </activity>    
        <activity 
            android:name=".FollowList"
            android:label="@string/title_activity_Urmarite" 
            android:parentActivityName="com.exploreca.tourfinder.MainActivity">            
        </activity>    


...

Try this.. 尝试这个..

change this.. 改变这个..

Intent mainActivity = new Intent("com.exploreca.tourfinder.MainActivity");
startActivity(mainActivity);

to

Intent mainActivity = new Intent(Splash.this,MainActivity.class);
startActivity(mainActivity);
public class SplashActivity extends AppCompatActivity {  

    Handler handler;
    private final int SPLASH_DISPLAY_LENGTH = 2000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        SplashStart();
    }

    private void SplashStart() {
        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();


            }
        }, SPLASH_DISPLAY_LENGTH);
    }


    @Override
    protected void onResume() {
        super.onResume();

    }
}

/* just add on resume method inside do not start your splace method in `onResume` method...*/

when you click back button actually the activity is getting finished and when you comes back the splash screen shows. 当您单击“后退”按钮时,活动实际上已完成,而当您返回时,将显示初始屏幕。 That because once more the appliaction is starting. 那是因为再一次开始应用。

When you click home button the activity is not finishing but it goes to background and when you open the app oncemoe then the same activity is bought to front. 当您单击“主页”按钮时,活动尚未结束,但会转到后台,而一旦您打开应用程序,则该活动将被购买。 so the splash screen is not shown. 因此不会显示启动屏幕。

its how when it works through the lifecycle of the application. 它在应用程序的整个生命周期中如何工作的。

Try to call finish(); 尝试调用finish(); method in th onPause of the activity. 暂停活动中的方法。 So it will be finished all the time. 因此,它将一直完成。 Or try adding noHistory to the activity in the manifest. 或尝试将noHistory添加到清单中的活动。 Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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