0

I have 2 activities, ActivityA and ActivityB

ActivityA has a TextView with the following text:

<a href="com.myapp.mydata.user/23">John</a> is a great person.

When a user clicks on the link, I get the event on the manifest and opens ActivityB.

I have some info displayed on ActivityB, and when I click the standard back button on android, the onCreate method of ActivityA is executed again, when its clear that is should not do that because I am just going back.

What are my options?

Daniel Benedykt
  • 6,496
  • 12
  • 51
  • 73

3 Answers3

1

When a user clicks on the link, I get the event on the manifest and opens ActivityB. Then this means you're putting ActivityB on top of the stack ... This is expected. What you could do is call finish() on ActivityA just before you open ActivityB. In this way you're removing ActivityA from the stack. When you'll go back from ActivityB then the app will display what was before ActivityA - if it was anything there.

In your case, ActivityA seems to be destroyed by Android system to reclaim resources (memory usually). Or maybe you have Developer Options checked and you have in there Don't keep activities checked.

EDIT: because of below reasonable and fair comment on correctness from @merendica, also the down-voter: In your ActivityA in onCreate() you know if the activity is recreated or not by checking if Bundle attribute is not-null:

if(savedInstance == null) {
// activity newly created
} else {
// activity re-created either because of screen rotation 
// or user returned at some point to this activity
}

So if the activity is recreated, don't call any logic that you're currently using.

Community
  • 1
  • 1
gunar
  • 14,660
  • 7
  • 56
  • 87
  • That's not what the OP wanted. – meredrica Oct 23 '13 at 13:06
  • excuse me ... can you please ellaborate on the comment and on the down-vote reason? – gunar Oct 23 '13 at 13:06
  • OP asked what he can do about the fact that onCreate is called, not how he gets rid of the old activity ('I am just going back'). Also, the title hints on "call onStart" only. Your answer is technically correct, but it's not applicable to the question :) – meredrica Oct 23 '13 at 13:10
  • 3
    ... and that deserved a downvote? :) Maybe the author of this question didn't put the correct question. You reacted as if someone asks: `What is the correct way of commiting suicide`? The technical **correct** answer would be: `Hanging, shot in the head, jump in a swamp full of crocs, etc`. But the reasonable answer would be: `Why would you commit suicide?` ... Same case here, but not so dramatical :) – gunar Oct 23 '13 at 13:22
  • I removed the downvote, but to borrow your example: it's the OP asking `why would somebody commit suicide?` and you answer `by hanging himself while shooting himself through the head with a crocodile`. It's simply not what he asked for :) – meredrica Oct 23 '13 at 19:28
0

That's exactly what has to happen. Android destroys the Activity for some reason (low ram for example) so it has to be recreated. If you don't want to lose your data in the activity implement State Saving. You cannot change the Android life cycle and working against it will lead to disaster.

meredrica
  • 2,563
  • 1
  • 21
  • 24
-1

When you Press Back First OnResume Method called and after that Oncreate will called for sure..But you can handle that behavior even.

Kalpesh Lakhani
  • 1,003
  • 14
  • 28