Reputation: 21
I've an app. When my app start, I show and splash screen. When my splash screen finish, I start my main activity. But I've a problem. When I rotate my app in splash screen, my app restart and four senconds later, start my new activity. If I rotate my splash screen three time, start three main activity.
I want start only one activity, how I can do it?
This is my splash screen code:
public class Splash extends Activity {
public static final int seconds=4;
public static final int miliseconds=seconds*1000;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
progressBar=(ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(seconds-1);
progressBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
startAnimation();
}
public void startAnimation(){
new CountDownTimer(miliseconds,1000){
@Override
public void onTick(long millisUntilFinished) {
//progressbar update--->1 second
progressBar.setProgress(getMiliseconds(millisUntilFinished));
}
@Override
public void onFinish() {
Intent i = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivityForResult(i,1);
}
}.start();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//when I return to the MainActivity, finish my app
if(requestCode==1) finish();
}
public int getMiliseconds(long milis){
return (int) ((milisegundos-milis)/1000);
}
}
Upvotes: 0
Views: 1280
Reputation: 20120
In AndroidManifest.xml
, you need to set the orientation to a specific orientation. Add in android:screenOrientation="portrait"
under the stuff for your activity. For example, my activity might look like this now:
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If you are in landscape mode, use android:screenOrientation="landscape"
.
If this doesn't work, in your onFinish()
method, use the following:
@Override
public void onFinish() {
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
finish();
}
Upvotes: 0
Reputation: 495
Use configchanges
in your activity tag in manifest.xml
Example
<activity
android:name=".Splash"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation" />
you can use this tag on another activities as well if you want to avoid onCreate again and again
Upvotes: 1
Reputation: 13302
Try this
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
finish();
Upvotes: 0