简体   繁体   中英

Android activity starting twice on startup

SplashActivity .java

public class SplashActivity extends Activity {

private static String TAG = SplashActivity.class.getName();
private static long SLEEP_TIME = 4; // Time to display the splash image for
                                    // in seconds.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes

    setContentView(R.layout.activity_splash);

    Log.d("man", "Starting app");
    testHttp test = new testHttp();
    test.test();
}

Here is the Manifest

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.rhino68mobileapp.SplashActivity"
        android:label="@string/title_activity_splash" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>    
</application>

When I run my app on my device this is what the logs read:

03-06 06:12:16.819: D/man(2105): Starting app
03-06 06:12:16.819: D/TEST(2105):  Starting test
03-06 06:12:16.869: D/TEST(2105):  make request    
03-06 06:12:16.869: D/man(2105): Starting app
03-06 06:12:16.869: D/TEST(2105):  Starting test
03-06 06:12:16.869: D/TEST(2105):  make request

Try this for your splash screen:

public class Splash extends Activity implements Runnable {

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

        Handler handler = new Handler();
        handler.postDelayed(this, 1500); // SLEEP_TIME in milis
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splash, menu);
        return true;
    }

    public void run(){
        startActivity(new Intent(this, Main.class)); // Call the after splash screen
        finish();
    }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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