2

i have two activities: A and B.

from A to B:

onCreate

Context context = this;

onClick

Intent i = new Intent(context,B.class);
startActivity(i);

from B to A: (if B has a button to return to A)

button.setOnClickListener(new View.OnClickListener(){
   public void onClick(View v){
      finish();
   }
});

But if the flow is like:

A call B, the user that is watching B clicks Home button and click again B from launched activities and clicks the button to call finish(), it goes to Home and not to my first activity A. How can i do it?

EDIT:

flow

  1. A->B // with startActivity(Intent);
  2. B->Home // clicking home button on device
  3. Home->B // clicking my app from launched activities
  4. B.finish() -> Home and not A // finish or onBackPressed do the same
Jayyrus
  • 12,961
  • 41
  • 132
  • 214

5 Answers5

1

can you try this :-

WHY ARE YOUR USING getApplicationContext() instead of use this or Activity A name.

A ->B

 Intent i = new Intent(yourcurrentActivity,B.class);
 startActivty(i);

B ->A

call finish()

update if you are using this:- remove it

android:noHistory="true";
Monty
  • 3,205
  • 8
  • 36
  • 61
  • i don't want to start again my A class.. i don't need to execute code inside onCreate – Jayyrus Mar 29 '13 at 10:05
  • u want to come at home or FirstActivity after finish() – Monty Mar 29 '13 at 10:07
  • FirstActivity.. but i don't need to create again FirstActivity but just bring it to front hiding the Second one – Jayyrus Mar 29 '13 at 10:08
  • no way.. (see edit) with myApplication instead getApplicationContext() it goes to home and not in firstActivity – Jayyrus Mar 29 '13 at 10:18
  • android:noHistory="false" in your manifest file with in your first activity or remove this....if u r using it. – Monty Mar 29 '13 at 10:21
  • i solved simply: Intent i = new Intent(getActivity(),MyFirstClass.class); getActivity().startActivity(i); getActivity().finish(); – Jayyrus Mar 29 '13 at 10:30
  • did you use this keyword or your activtity name instead of getApplicationContext..try this once because getActivity not a good idea. – Monty Mar 29 '13 at 10:33
  • that is a PreferenceFragment, i need to call getActivity :) – Jayyrus Mar 29 '13 at 10:43
0

call

onBackPressed();

instead of

finish();
Ercan
  • 3,705
  • 1
  • 22
  • 37
  • sorry... but onBackPressed() do the same as finish() -> A->B, B->Home,Home->B,(B.onBackPressed() || B.finish()) -> Home and not A – Jayyrus Mar 29 '13 at 10:09
0

Try this:

startActivity(new Intent(B.this,B.class));

and finish() in activity A as

// in your activity A
Intent i = new Intent(getApplicationContext(),B.class);
startActivity(i);
finish();

and

public void OnBackPressed()
{
finish();
}
Linga
  • 10,379
  • 10
  • 52
  • 104
0

Try this:

public void startActivityForResult (Intent intent, int requestCode)
stefan
  • 1
0

check below code: i have 3 activity : MainActivity, A_Acvitiy, B_actiivty (Hope its worked as per your req..)

package com.example.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void callActivity(View v) {
        if (v.getId() == R.id.button1) {
            // A
            startActivity(new Intent(MainActivity.this, A_activity.class));
        }

        if (v.getId() == R.id.button2) {
            // B
            startActivity(new Intent(MainActivity.this, B_activity.class));
            if (GeneraClass.isFormB) {
                            GeneraClass.isFormB = false;
                finish();
            }
        }
    }
}

A_activity:

package com.example.androidtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class A_activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_layout);
    }

    public void callActivity(View v) {
        if (v.getId() == R.id.button1) {
            // B
            startActivity(new Intent(A_activity.this, B_activity.class));
                 finish();
        }

    }

}

b_activity:

package com.example.androidtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class B_activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.b_layout);
    }

    public void callActivity(View v) {
        if (v.getId() == R.id.home) {
            // home
            GeneraClass.isFormB = true;
            startActivity(new Intent(B_activity.this, MainActivity.class));
            finish();
        }

    }
}

my extra class:

package com.example.androidtest;

public class GeneraClass {

    public static boolean isFormB = false;
}

So, Home-->A(finish)-->B--->Home(if from b then finish)-->B-->Back--> Home --->Back--> exit App

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177