Elad Benda2
Elad Benda2

Reputation: 15452

how can activity start a new activity and close itself?

I have an android view which calls another view and should be closed.

I use this code

        public void onClick(View v) {
            Intent intent = new Intent(A.this, B.class);
            startActivityForResult(intent,1);
            setResult(RESULT_OK);
            finish();               
        }

and I see after Activity B is on and calls finish, Activity A appears

and i understand it has never been closed.

How can I resolve this?

Upvotes: 0

Views: 1028

Answers (6)

jagmohan
jagmohan

Reputation: 2052

Your are starting activity B for result from activity A

public void onClick(View v) {
    Intent intent = new Intent(A.this, B.class);
    startActivityForResult(intent,1);
    setResult(RESULT_OK);
    finish();               
}

So activity A is expecting result to delivered by activity B when activity B finishes.

Now, to get around this problem, start activity B in onActivityResult() method of the activity from which you started activity A.

// modified onClick method
public void onClick(View v) {
    // simply set result to OK and finish activity A
    setResult(RESULT_OK);
    finish();
}

In the activity that starts activity A, define two constants or you can have a separate Java class to define constants and refer them in this class.

public static final int REQUEST_CODE_FOR_A = 100;
public static final int REQUEST_CODE_FOR_B = 101;

Override the onActivityResult() method in the activity that starts activity A and start activity B as follows

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if(requestCode == REQUEST_CODE_FOR_A) {
            // start activity B when activity returns RESULT_OK
            Intent intent = new Intent(this, B.class);
            startActivityForResult(intent, REQUEST_CODE_FOR_B);
        } else if (requestCode == REQUEST_CODE_FOR_B) {
            // handle the result from Activity B
        }
    }
}

Hope this helps.

Upvotes: 0

Neha - Systematix
Neha - Systematix

Reputation: 96

public void onClick(View v) {
            Intent intent = new Intent(A.this, B.class);
            startActivity(intent);
            finish();               
        }

Upvotes: 0

suitianshi
suitianshi

Reputation: 3340

Try the following code:

public void onClick(View v) {
        Intent intent = new Intent(A.this, B.class);
        startActivity(intent);
        finish();               
    }

Upvotes: 0

Naveed Ahmad
Naveed Ahmad

Reputation: 6737

You have to use Intent Where you wan to Start new Activity

Intent intent=new Intent(MainActivity.this,newActivity.class);
startActivity(intent);

and you have to override MainActivity onPause()

    @Override
protected void onPause() {
    finish();
}

Upvotes: 0

SKT
SKT

Reputation: 1851

Call finish() before starting the activity

public void onClick(View v) {
            Intent intent = new Intent(A.this, B.class);
            finish();               
            startActivityForResult(intent,1);
            setResult(RESULT_OK);
        }

Upvotes: 0

stefana
stefana

Reputation: 2626

Try

public void onClick(View v) {
    Intent intent = new Intent(A.this, B.class);
    A.this.finish();   
    startActivity(intent);    
}

Upvotes: 1

Related Questions