【问题标题】:Splash screen activity ignored when resuming application - Android恢复应用程序时忽略启动屏幕活动 - Android
【发布时间】:2014-04-25 10:46:25
【问题描述】:

我想知道为什么我的应用程序在恢复应用程序时忽略了我的 SplashScreen.java 活动。如果我用“返回”按钮关闭它,启动时会出现启动画面,但如果我用主页按钮退出,则不会调用 SplashScreen 活动...:(

我什至添加了 onResume 事件,但在恢复我的应用程序时仍然不会出现启动画面。 谢谢!!

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

}

梅菲斯特:

...

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


...

【问题讨论】:

  • 您希望在用户启动/返回您的应用时始终显示启动画面?
  • if I exit with the home button,不,你不要退出。您将应用置于后台。
  • 惹恼自己的用户是相当错误的策略。您应该仅在应用启动时显示启动画面。顾名思义,恢复应该在用户离开的地方恢复应用。
  • @Marcin Orlowski - 也许你是对的,谢谢。

标签: android android-activity splash-screen onresume


【解决方案1】:

试试这个..

改变这个..

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

到

Intent mainActivity = new Intent(Splash.this,MainActivity.class);
startActivity(mainActivity);

【讨论】:

  • 嗨@Hariharan - 我已经添加了,但没有区别。 :(
【解决方案2】:
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...*/

【讨论】:

    【解决方案3】:

    当您单击返回按钮时,活动实际上已经完成,当您返回时,启动屏幕会显示。那是因为应用程序再次启动。

    当您单击主页按钮时,活动尚未完成,但它进入后台,当您打开应用程序一次时,相同的活动被购买到前面。所以不显示启动画面。

    它在应用程序生命周期中的工作方式。

    尝试在活动的 onPause 中调用finish(); 方法。所以它会一直完成。或者尝试将noHistory 添加到清单中的活动中。希望这会对你有所帮助。

    【讨论】:

    • 感谢您的建议!
    • 它不是管理应用程序的正确方法,我猜那是我得到了一个-1,所以当你这样做时,请注意它不是解决这个问题的正确方法,即使这满足了你的要求.应用程序不应一次又一次地启动。
    猜你喜欢
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多