0

I'm new with Xamarin and I'm tring to create an Android App.

I have created a 2 Views related to 2 different Activities. The first Activity, name it A, has a button that launches the second Activity, name it B.

B has an EventHandler that is connected, in the OnCreate method, to A's Event. The EventHandler print on console a string, that's it.

if I launch the app and press the A's button, the B activity appears. Now, if I press the Back button the A Activity appears again, after that I press the button again and the B Activity appears but this is a new B Activity istance and not the previous one. I can see that because the OnCreate method is called twice and because I can see on console that the event is called twice.

If I repeat this many times I can see the same string printed on console repeated many times. I would like to have only one instance of any activity, so I need to change view without creating more istances of one activity that was already created or destroy the B Activity when the Back button is pressed.

How can I do that? is it the right way to do it?

Gappa
  • 757
  • 2
  • 16
  • 34

2 Answers2

0

You can't link the two activities, or better, you really shouldn't. If you need shared data then go through static properties (perhaps in a separate static class). Read about activity lifecycle and activity launch modes. Note that you are probably creating a wrong flow though.

Miha Markic
  • 3,158
  • 22
  • 28
  • How can I `Resume` an Activity? I already tested the various _Launch Mode_ but it didn't work. – Gappa Oct 09 '14 at 09:14
0

I think in order to get what you want (having two "Activities" of which the instance is always the same when you are switching between them), you basically need to use Fragments instead of Activities, in a FragmentPagerAdapter.

http://developer.xamarin.com/guides/android/platform_features/fragments/part_1_-_creating_a_fragment/

FragmentPagerAdapter Exists Only In Android.Support.V4.App (and not Android.App)

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • Thinks that `A` is a menu, in which you can go throw different things, and `B` is one of those things that opens up when the user selects a button in the menu activity.. So I will not have only 2 activities but _n_ . – Gappa Oct 09 '14 at 09:12