Start new Activity and finish current one in Android?
You can use finish()
method or you can use:
android:noHistory="true"
And then there is no need to call finish()
anymore.
<activity android:name=".ClassName" android:noHistory="true" ... />
How to finish current activity and launch a new activity Rx way?
Actually you are calling it in a wrong order. You should call startActivity()
first before calling finish()
. Still, encapsulating this process inside an Observable might introduce an unexpected behavior, because startActivity()
is supposed to be called on UI thread as it triggers UI animation/transition to another Activity.
What you want to do is to call both startActivity()
and finish()
inside onNext()
or onComplete()
callback. But in case you REALLY need to encapsulate it, you can follow these simple steps:
1. Create a method which handles Activity switching
private fun launchNewActivity() {
startActivity(Intent())
finish()
}
2. Encapsulate this method in Observable
Observable.fromCallable(this::launchNewActivity)
You will want to put above Observable inside a CompositeDisposable
or a DisposableObserver
.
How to finish Activity when starting other activity in Android?
You need to intent
your current context
to another activity first with startActivity
. After that you can finish
your current activity
from where you redirect.
Intent intent = new Intent(this, FirstActivity.class);// New activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
- Clears the activity stack. If you don't want to clear the activity stack. PLease don't use that flag then.
Finish Current Activity and start a new one
try this code for start browser and clear all the stacks of your application
Intent intent = new Intent(); // need to set your Intent View here
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getActivity().startActivity(intent);
getActivity().finish();
Updated
Try this attribute in your activity in AndroidManifiest.xml file
<activity android:name=".MainActivity"
android:excludeFromRecents="true" ...
How to start new activity from current activity?
It's better to do not leave a toast message with a finished activity. It may cause some problems like Screen Overlay Detected error. So, do this:
In MainActivity:
val intent = Intent(this, Main2Activity::class.java)
intent.putExtra("SHOW_WELCOME", true)
startActivity(intent)
finish()
In Main2Activity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent.getBooleanExtra("SHOW_WELCOME", false)) {
Toast.makeText(this, getString(R.string.welcome), Toast.LENGTH_LONG).show()
}
}
Android finish Activity and start another one
Use
Intent intent = new Intent(SyncActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Finish old activity and start a new one or vice versa
When you do startActivity(), all that does is post your intent in a queue of events. The actual starting of the activity happens asynchronously in the near future. So I don't see a big difference between the two.
Call new activity and destroy current one
Intent i = new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
finish();
Finish current activity and start another one after share intent
I solved it by starting the the new activity then finishing the current activity then starting twitter app and share my tweet:
Intent i = new Intent(context,SecondActivity.class);
startActivity(i);
getActivity().finish();
getActivity().getPackageManager().getPackageInfo("com.twitter.android", 0);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName("com.twitter.android", "com.twitter.android.composer.ComposerActivity");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, url);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivityForResult(intent,0);
How to finish current activity in Android
If you are doing a loading screen, just set the parameter to not keep it in activity stack. In your manifest.xml, where you define your activity do:
<activity android:name=".LoadingScreen" android:noHistory="true" ... />
And in your code there is no need to call .finish() anymore. Just do startActivity(i);
There is also no need to keep a instance of your current activity in a separate field. You can always access it like LoadingScreen.this.doSomething()
instead of private LoadingScreen loadingScreen;
Related Topics
How to Retrieve HTML Content from Webview (As a String)
Opencv Image Comparison in Android
Switching Between Fragment View
Android Sax Parser Not Getting Full Text from Between Tags
Video Compression on Android Using New Mediacodec Library
Add an Array of Buttons to a Gridview in an Android Application
R.Java Disappears After Project Clean
Difference Between Addvalueeventlistener() and Addlistenerforsinglevalueevent() of Firebase
Android: Outofmemoryerror: Bitmap Size Exceeds Vm Budget with No Reason I Can See
How to Create a New Avd in Eclipse
React-Native :Java.Lang.Unsatisfiedlinkerror: Couldn't Find Dso to Load: Libhermes.So
Cardview Inside Recyclerview Has Extra Margins
Swiperefreshlayout + Webview When Scroll Position Is at Top
Custom Title Bar Without Padding (Android)
Gradle Flavors for Android with Custom Source Sets - What Should the Gradle Files Look Like
Android: Total Height of Scrollview
Xamarin Forms Android App Crashes Running Debug with VS Android Emulator