有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java splashscreen和第二个活动返回

我正在使用Android studio编写一个简单的应用程序,它会打开主活动,当我点击按钮时,它会产生新的意图,以便在web浏览器中启动URL。打开URL的代码是:

String Link=listURL.get((int) id).toString();
                Uri uriUrl = Uri.parse(Link);
                Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
                startActivity(launchBrowser);

它工作正常,显示网络结果,我可以使用后退按钮返回主要活动。在我决定使用闪屏之前,一切都很好。 在实现了一个启动屏幕后,当我点击web brower中的后退按钮时,它将退出应用程序。它不再出现在“主要”活动中

这是我的舱单。xml: 如果我试图移除。splashscreen节,一切正常。我没有启动屏幕,但一切正常。可能是舱单的属性问题吗

<?xml version="1.0" encoding="utf-8"?>

<uses-permission 安卓:name="安卓.permission.INTERNET" />

<application
    安卓:allowBackup="true"
    安卓:icon="@mipmap/ic_launcher"
    安卓:label="@string/app_name"
    安卓:theme="@style/AppTheme" >

    <activity
        安卓:name=".SplashScreen"
        安卓:theme="@安卓:style/Theme.Translucent"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />
            <category 安卓:name="安卓.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        安卓:name=".MyActivity"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAINACTIVITY" />
            <category 安卓:name="安卓.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

这里是我的启动屏幕:

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

    Thread timerThread = new Thread(){
        public void run(){
            try{
                sleep(1000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent intent = new Intent(SplashScreen.this,MyActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }
    };
    timerThread.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

谢谢 乔瓦尼


共 (2) 个答案

  1. # 1 楼答案

    delete finish() from onpause and write your timer code inside onresume it will work
    Ex: 
    onresume()
    {
     Thread timerThread = new Thread(){
            public void run(){
                try{
                    sleep(1000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent intent = new Intent(SplashScreen.this,MyActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        };
        timerThread.start();
    }
    
  2. # 2 楼答案

    我认为这对你很有用

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    

    意图之后

    finish();