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;
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.
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 go back to previous fragment?
use start activity for result
Intent intent=new Intent(getActivity(), SkillActivity.class);
Bundle bundle = new Bundle();
bundle.putString("caller","editprofile");
bundle.putParcelableArrayList("userskills", (ArrayList<Skill>) userskills);
intent.putExtras(bundle);
startActivityForResult(intent,122);// you can change this number
and in your activity which hold the fragment add this implementation to pass the result to the fragment
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
and in Done button
btnDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent returnIntent = new Intent();
Bundle args = new Bundle();
args.putParcelableArrayList("newskills",
(ArrayList<Skill>)newskills);
returnIntent.putExtras("result", args);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
finaly implment this in your fragment to recive the data
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 122 && resultCode == Activity.RESULT_OK) {
Bundel bundle=data.getExtras();
String caller=data.getString("caller");
ArrayList<Skill> skills=data.getParcelable("userskills");
}
}
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" ...
Back button inside of ActionBar how to finish current Activity
Just do this:
getActionBar().setDisplayHomeAsUpEnabled(true);
on your onCreate() Method. Then put this inside your activity class too:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The finish()
function closes your current activity and navigates to the last visited activity in your application, in short words it goes back.
Android close current activity and return to previous activity
Update your first activity (FirstActivity.java)
FirstActivity.java
Use startActivityForResult(uninstallIntent, 1); //1 is REQUEST_CODE
After unintalling app, FirstActivity.onActivityResult will be automatically called, you can use this method to do something.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// un-installed successfully
finish();
}
else {
// failed to un-install
}
}
}
Related Topics
Stuck with Getting Camera Pic When Using the Tab Activity
Value of a Global Variable Is Reset After It Is Initialised in Valueeventlistener
No Adapter Attached; Skipping Layout
Get Nfc Tag with Ndef Android Application Record (Aar)
Error: Failed to Resolve: Com.Android.Support:Appcompat-V7:29.0.1'
Android How to Replace the Deprecated Tabhost
Android: How to Track Down the Origin of a Inflateexception
Android: Permission Denial: Starting Intent with Revoked Permission Android.Permission.Camera
How to Read Contacts in Android Using Realm
How to Decompile an APK or Dex File on Android Platform
Change Keyboard Input Language
How to Ask Runtime Permissions for Camera in Android , Runtime Storage Permissions
Streaming Audio from a Url in Android Using Mediaplayer
How to Use the Android Volley API
Listen to Own Application Uninstall Event on Android
Unzip a Zipped File on Sd Card in Android Application