2

I realize there are several topics about this issue, but none provides a working solution for me, so I'm posting this problem again and hopefully can get some solutions/suggestions.

So what's happening is, I have an application that contain 2 activities. The first one is for Login, for now I haven't implemented the login feature and all it does is click a button and the second activity gets launched. The second activity then displays a map, centered by user current location. It also contains a logout button to go back to the 1st login activity.

Now the login activity is the LAUNCHER activity because I apparently want user to login when they first open this app. Then after login button clicked I start the 2nd map activity and finishes the 1st.

However, if I leave the 2nd map activity by hitting home button, The map activity is put to run in the background. Now, if I open this app from the 'recent' opened app list, it will go back to the 2nd map app, which is desired. But if I open from the app list on android desktop. then it will launch a new instance of 1st login activity, instead of resume to where I left (the 2nd map activity). And the map activity is just behind it in the stack (I think) because in the newly launched login activity, if I hit back, it actually return to the map activity as where I left it.

in short, I wish to launch the app from the saved instance of this app, if there is one. I have seem this and it's not quite my problem.

I hope this is a clear description of my problem. Here are the relevant codes. I'm using Mono for Android to code, but I don't think it matters.

Login : Activity

        loginButton.Click += delegate 
        {
            StartActivityForResult(typeof(MapDemo), 0);
            Finish();
        };

MapDemo : MapActivity

        logoutButton.Click += delegate
        {
            var intent = new Intent();
            SetResult(Result.Ok, intent);
            Finish();

        };

Here are some links I found that has similar problem as I do.

Start activity after Resume
http://forum.xda-developers.com/showthread.php?t=856386
Android Resume Activity

I realize it might be because of I'm using the StartActivityForResult method incorrectly. It is a bit different using mono, but if you have a guess about what my problem might be, please point it out for me. Thank you a lot!

Community
  • 1
  • 1
lynnyilu
  • 573
  • 1
  • 5
  • 15

2 Answers2

0

okay since there is no answer to this question, I figured out a very simple (and maybe-not-a-good-practice-but-does-work solution)

basically I set both activity to singleTask. So only one instance can be created. then I keep a static variable in 1st activity for my second activity. OnCreate of 2nd activity I set it to true and every Finish() of 2nd activity is preceded by setting this variable to false.

and onRestart of 2nd activity I just check this is2ndActivityRunning variable. if it is, i use startActivity() directly and since there is only one instance of 2nd activity will be created it will find the old state where I left it. If not, the 1st activity is displayed as normal.

I'm sure this won't be the best way to do it.. but I guess for my simple app.. as long as it works..

lynnyilu
  • 573
  • 1
  • 5
  • 15
0

You might consider something like a start-up activity that is either the splash screen or has no layout resource at all.

I bootstrap applications this way. In the code, you could detect whether the user is logged in and conditionally show the login screen.

If you don't want the user to be able to go back to the splash/start activity by using the back button, you can use something like this (which I believe I got from another StackOverflow answer):

    public override void OnAttachedToWindow()
    {
        var finishTime = DateTime.Now.AddMilliseconds(3000);

        base.OnAttachedToWindow();
        new Thread(() =>
        {
            while (DateTime.Now < finishTime) Thread.Sleep(100);
            RunOnUiThread(Finish);
        }).Start();
    }

This will take that activity off the stack after three seconds.

manadart
  • 1,750
  • 11
  • 13
  • this is similar to what I've got now I guess, to check whether condition login is satisfied then decide which activity to show. It's also very nice of you to mention about the back bottom. I will give a try. Thanks for suggesting! – lynnyilu Jan 27 '12 at 03:00