I am launching a new activity using the following:
Intent intent = new Intent(this, MyNewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(0, 0);
While MyNewActivity
is launching, a black screen is shown.
If I remove Intent.FLAG_ACTIVITY_CLEAR_TASK
, the activity is launched without showing a black screen at any moment (instead, the previous activity is shown while the new one loads).
Is there any way to avoid this black screen? Removing the flags seems to not be an option (I need to clear all the current task's stack and launch a new activity as the root one).
EDIT: I attach a very simple code which reproduces the issue (set a dark theme such as Theme.AppCompat
for the app). The black screen is shown for very little time (depends on how many work the receiving activity does when launching), but you can see it. If you don't use FLAG_ACTIVITY_CLEAR_TASK
, the black screen is not shown and the transition is smooth:
MainActivity
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyNewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(0,0);
}
});
}
}
MyNewActivity
public class MyNewActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK ME!" />
</RelativeLayout>
activity_new.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am the new activity!" />
</RelativeLayout>
Thanks!
onCreate()
. Offload the stuff into a background thread that you start inonCreate()
instead. – RiggleIntent.FLAG_ACTIVITY_CLEAR_TOP
instead ofCLEAR_TASK
. This will only work if you leave the original instance ofMyNewActivity
in the stack (ie: don't finish it when you launch other Activities). If this still doesn't fix it, you can tryIntent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
. This will clear all other activities off the stack and return to the existing instance ofMyNewActivity
. In this case, you could add an "extra" to theIntent
, whichMyNewActivity
can check inonNewIntent()
and if set, it could reinitialize itself. – RiggleIntent
launch modes,FLAG_ACTIVITY_CLEAR_TOP
is not useful in my case as I could have never launchedMyNewActivity
, and I want to clear all the previous activity stack. For example, I have the following flow:A
->B
->C
->D
->MyNewActivity
I want to erase from my activity stack theA
,B
,C
andD
activities, and launchMyNewActivity
. The only flag that does what I want isIntent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK
. At least, that is the only one I'm aware of. – AlfyFLAG_ACTIVITY_CLEAR_TASK
and instead callfinish()
after starting new activity. this link helped me a lot. – Gimcrackery