¿Cómo volver a otra actividad incluso si no se creó en Android?
Frecuentes
Visto 142 veces
0
Assume I have two activities.
Activity A and Activity B, while Activity A is main activity.
There is a button in Activity A that will go to Activity B.
So, Activity A -> click -> Activity B and then click back button, it will go back to Activity A.
However, there is another story that I will have a GCM notification.
When clicking the notification, it will immediately go to Activity B.
Pero el problema es:
- If the application already run, when I click the notification and go to Activity B, and then click back button, it will correctly go back to Activity A.
- If I kill the application, and click the notification and go to Activity B, and then click back button, it will go to the Home Screen. But the problem is I want it will go back to Activity A.
¿Cómo resolver este problema?
2 Respuestas
2
Override you onBackPressed method and start the activity A using an intent.
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
// Start Activity A heres
}
Respondido 24 ago 12, 10:08
if Activity A already created, will it have some problems if I start the activity again? - jjlin
You can kill Activity A when moving to Activity B to ensure that you wont have that extra Activity. Just add finish(); after startActivity(). - Ninja perezoso
There is another way to find out the running activities too. You can try to see if Activity A is running or not. - Ninja perezoso
but the case is that I want to go back to Activity A if it exists, otherwise just create it. How to check? - jjlin
There are different ways to do it as listed here stackoverflow.com/questions/5446565/…. Like using a static value, .... - Ninja perezoso
0
@Override
public void onBackPressed() {
// Don't forget to remove super.onBackPressed()
startActivity(new Intent(ActivityB.this,ActivityA.class));
}
Respondido 24 ago 12, 10:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android or haz tu propia pregunta.
This might confuse the users. Goes against the Android "Pattern" . You could provide a separate button to go to Activity A? And let the Back Press do what it does. - Sameer
override onBackpressed in your Activity B and start your activity A from there then it will work in both use cases. - Sankar