How to Finish Activity When Starting Other Activity in Android

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);

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.

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.

Finish an activity from another activity

  1. Make your activity A in manifest file: launchMode = "singleInstance"

  2. When the user clicks new, do FirstActivity.fa.finish(); and call the new Intent.

  3. 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.

Finish all previous activities from other activity

try this solution..

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

hope it helps.

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 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!

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 do I go back to the finished activity dynamically?

You should use Stack to keep track of your Activities.
Stack in Android

Just push the current Activity before Intent to next Activity, and pop the current Activity in the onBackPressed() method.

You can save the Stack on SharedPreference.

EDIT:

This is the code. I used a DataHandler class to save the Stack.

DataHandler.java

public class DataHandler {
private static DataHandler mDataHandler;
private Deque<Intent> stack = new ArrayDeque<>();

private DataHandler() {
}

public static DataHandler getInstance(){
if(mDataHandler == null){
mDataHandler = new DataHandler();
}
return mDataHandler;
}

public Deque<Intent> getStack() {
return stack;
}

public void setStack(Deque<Intent> stack) {
this.stack = stack;
}
}

M1Activity.java

public class M1Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_m1);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(M1Activity.this,M2Activity.class);
startActivity(intent);
DataHandler.getInstance().getStack().push(intent);
finish();
}
});
}

@Override
public void onBackPressed() {
super.onBackPressed();
if (DataHandler.getInstance().getStack().size() > 0){
Intent intent = DataHandler.getInstance().getStack().pop();
startActivity(intent);
}
}
}

Just like M1Activity, you have to push and pop intent from Stack in every Activity.

Start second activity after finish the first activity

You can do this with passing intent with bundle from one activity to another at every single completion of activity. do the same thing in the second activity to start first one.

Intent i = new Intent(firstactivity.this,secondactivity.class);
Bundle b= new Bundle();
bundle.putStringArrayList("list",list); // Here you have to pass your list data
i.putExtras(bundle);
startActivity(i);


Related Topics



Leave a reply



Submit