Vernux
Vernux

Reputation: 1

splashscreen and second activity back

I'm writing a simple app using Android studio which open main acitivity and when I click on button it make new intent in order to launch URL in web browser. The code to open URL is:

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

It works fine, it shows the web results and I can come back to the main activity using the back button. All worked fine until I've decided to use a splash screen. After implemented a splash screen, when I click on back button in web brower, it exit from app. It doesn't come back no more in the "main" activity.

here my manifest.xml: If i try to remove the .splashscreen stanza, all works fine. I have'nt the splash screen but all works. Could be a manifest's properties problem?

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

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

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

    <activity
        android:name=".SplashScreen"
        android:theme="@android:style/Theme.Translucent"
        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=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAINACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

Here my splash screen:

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

}

thanks Giovanni

Upvotes: 0

Views: 129

Answers (4)

Vernux
Vernux

Reputation: 1

Thank you for your suggestions but it didn't solved. I've solved using another new and not predefined activity and all works fine.

Giovanni

Upvotes: 0

Nirav Shah
Nirav Shah

Reputation: 67

i think its works for you

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

after intent

finish();

Upvotes: 0

Siddharth Vanakudari
Siddharth Vanakudari

Reputation: 47

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

Upvotes: 1

Androider
Androider

Reputation: 3873

call finish() after execution of statement causes starting MyActivity

 Intent intent = new Intent(SplashScreen.this,MyActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
 finish();

Avoid calling finish() in onPause().

Upvotes: 1

Related Questions