Finish an activity from another activity
Make your activity A in manifest file:
launchMode = "singleInstance"
When the user clicks new, do
FirstActivity.fa.finish();
and call the new Intent.When the user clicks modify, call the new Intent or simply finish activity B.
FIRST WAY
In your first activity, declare one Activity object
like this,
public static Activity fa;
onCreate()
{
fa = this;
}
now use that object in another Activity
to finish first-activity like this,
onCreate()
{
FirstActivity.fa.finish();
}
SECOND WAY
While calling your activity FirstActivity
which you want to finish as soon as you move on,
You can add flag while calling FirstActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
But using this flag the activity will get finished evenif you want it not to. and sometime onBack
if you want to show the FirstActivity
you will have to call it using intent.
Is there a better way to finish the activity from another activity?
- Is there a better way to finish the activity from another activity?
Yes, What you have done is used static
instace on Activity
which could easily lead to memory leak issues. Its always good practice to use static
keyword only if necessary and very carefully as well.
Better way would use BroadCastreceiver
So, what you need to do is create broadcast receiver in your first and second activity and whenver you wants to finish those activity. use sendBroadCast(..)
method.
you need to search it out how Broadcastreceiver
works in android.
- Is this way is correct?
No, The way you did is not correct.
Finish activity from another activity
In your onCreate()
method assign a static instance to a variable to create a Singleton:
public static ActivityA instance = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
}
@Override
public void finish() {
super.finish();
instance = null;
}
then in C:
public void onCreate(Bundle savedInstanceState) {
if(ActivityA.instance != null) {
try {
ActivityA.instance.finish();
} catch (Exception e) {}
}
}
(Repeat above code for B as well as A)
I would say this is NOT elegant and is bound to introduce strange lifecycle bugs.
It would be better if you could tweak your requirement - if you can't you could perhaps use a single Activity and change A, B and C into Fragments
?
I would have suggested a Broadcast but as I understand it you can't have instance level broadcast receivers on "non resumed" Activities.
Using a Service
to bind to the Activities seems like overkill - but you might want to consider that if the above doesn't work.
Good luck!
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.
Killing one activity from another
You could try to directly kill an activity by calling a static method in that activity:
Activity A should have a variable
static ActivityA activityA;
In onCreate state:
activityA = this;
and add this method:
public static ActivityA getInstance(){
return activityA;
}
In activity B, call the function getInstance()
ActivityA.getInstance().finish();
Close another activity from current one
Here is how I've solved the problem:
Activity A:
//Start Activity B
startActivityForResult(new Intent(this, B.class), 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
startActivity(new Intent(this, C.class));
finish();
}
}
Activity B:
//back button press:
setResult(RESULT_CANCELED, new Intent());
finish();
//start Activity C button:
setResult(RESULT_OK, new Intent());
finish();
Hope it will help someone.
android how to finish an activity from other activity
just finish the second activity when you open third activity
suppose in second activity on some button click you are opening third activity using start activity;
startActivity(intent);
finish();//this will finish second activity and open third activity so when you press back from third activity it will open first activity.
if you want depended on some condition then on activity
setResult(123);
something code like this
now when override onActivityResult in second activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==123){
//finish
}
}
also make sure one thing you need to use startActivityForResult(intent, requestCode); for result in second activity to start third activity.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity2 extends Activity{
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(new Intent(Activity2.this,Activity3.class)), 12);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==123 && requestCode==12){
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity3 extends Activity{
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(123);
}
});
}
}
Finish android activity from another with Kotlin
The error Expecting member declaration
is there because you wrote a statement (the function call) inside a class. In that scope, declarations (functions, inner classes) are expected.
You have to place your statements inside functions (and then call those from somewhere) in order for them to be executed.
Close activity from another activity through an intent
this may help you...
public class First extends Activity {
public static final String ACTION_CLOSE = "yourPackageName.ACTION_CLOSE";
private FirstReceiver firstReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
IntentFilter filter = new IntentFilter(ACTION_CLOSE);
firstReceiver = new FirstReceiver();
registerReceiver(firstReceiver, filter);
Intent close = new Intent(getApplicationContext(), Close.class);
startActivity(close);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(firstReceiver);
}
class FirstReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("FirstReceiver", "FirstReceiver");
if (intent.getAction().equals(ACTION_CLOSE)) {
First.this.finish();
}
}
}
}
public class Close extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_close);
Intent myIntent = new Intent(First.ACTION_CLOSE);
sendBroadcast(myIntent);
Log.e("onCreate", "onCreate");
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.close, menu);
return true;
}
}
Related Topics
How to Adjust Layout When Soft Keyboard Appears
Best Practice: Asynctask During Orientation Change
How to Hide Action Bar Before Activity Is Created, and Then Show It Again
Calling Startactivity() from Outside of an Activity Context
In Android, How to Set Margins in Dp Programmatically
No Matching Client Found for Package Name (Google Analytics) - Multiple Productflavors & Buildtypes
How to Mock Location on Device
Get Content Uri from File Path in Android
Calculate Compass Bearing/Heading to Location in Android
Scroll Recyclerview to Show Selected Item on Top
Android New Bottom Navigation Bar or Bottomnavigationview
Android Studio Gradle Build Speed Up
Asynctask Won't Stop Even When the Activity Has Destroyed