Activity finish() question ??

by Neo » Mon, 27 Apr 2009 10:59:54 GMT


Sponsored Links
 hi guys

    I have a question about finish activity

    When start the activity i will set the activity object to the
Application, and i will registry it in the Appication
    app = MyApplication.getApplication(this);

    1. Start Activity A  store A id 0x12345678
    2. Activity A -> Activity B
    3. Store B id 0x87654321
    4. Finish B activity
    5. but the Activity B object not null in the Application

    i don't not why this problem???

--~--~---------~--~----~------------~-------~--~----~



Activity finish() question ??

by Dianne Hackborn » Mon, 27 Apr 2009 11:12:07 GMT


 If you are explicitly setting it, you will need to explicitly clear it at
some point, possibly in onDestroy().

Note that you need to be very careful when doing this kind of thing.
Generally the most safe global to store like this is the "currently resumed"
activity, set in onResume() and cleared in onPause().






-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Activity finish() question ??

by Neo » Mon, 27 Apr 2009 14:45:18 GMT


 public static MyApplication getApplication(Activity activity) {
        // TODO should this be synchronized?
        if (app == null)
        {

            initialize(activity);
        }


        mActivity = activity;
        return app;
    }
I means if i finish the Activity B, The Activity B object(mActivity)
is not null
--~--~---------~--~----~------------~-------~--~----~



Activity finish() question ??

by Dianne Hackborn » Mon, 27 Apr 2009 15:57:24 GMT


 You aren't supplying enough code to understand what you are doing (who is
calling getApplication()?).  But I'll see again -- if you don't see the
pointer to null, then yes, you will never get a null back.






-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--~--~---------~--~----~------------~-------~--~----~



Activity finish() question ??

by Neo » Tue, 28 Apr 2009 09:31:31 GMT


 Application class
public class MyApplication extends Application{
        public static MyApplication getApplication(Activity activity)
{
        // TODO should this be synchronized?
        if (app == null)
        {
            initialize(activity);
        }
        mActivity = activity;
        return app;
    }
}

Activity A class:
public class AActivity extends Activity{
    public void onCreate(Bundle savedInstanceState){
    app = MyApplication.getApplication(this);
    }
}

Activity B class:
public class BActivity extends Activity{
    public void onCreate(Bundle savedInstanceState){
    app = MyApplication.getApplication(this);
    }
}

I set the activity point to the application, because i want create
play sound function in the Application class.

1.when i onCreate() Activity A, i will set A point to the mActivity,
and create B point to mActivity.

2.finish() B Activity, back to A onResume(),but i don't set the A
point to mActivity.

3.Play sound, call the function in the Application,

    public void playAudio() {
       mMediaPlayer = MediaPlayer.create(mActivity, R.raw.type);
       mMediaPlayer.start();
    }

the mActivity is B point, i know because i havn't reset point, but i
finish the B activity, why i can play the sound used the B point
--~--~---------~--~----~------------~-------~--~----~



Activity finish() question ??

by Dianne Hackborn » Tue, 28 Apr 2009 10:04:35 GMT


 Okay, that confirms what I have been saying -- you set the value twice, once
when A was created, and once when B was created.  You finish B.  You haven't
changed the field.  It still points to B.  What else do you expect?

Btw, I will say again, doing this kind of thing is dangerous unless you have
a good understanding about activity lifecycles and all of the edge cases
that can happen; you are not dealing with a lot of things here (clearly just
the switch back from B to A).

Why are you even doing this at all?  Why not just create the MediaPlayer in
Application and play from there?  That would be a lot safer.  Better yet,
have a static function that you give the Application to; then you don't need
to have your own Application class at all.






-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--~--~---------~--~----~------------~-------~--~----~



Activity finish() question ??

by Neo » Wed, 29 Apr 2009 09:45:34 GMT


 because if i create media used the Application point(), i can't hear
the sound

    public void playAudio(int type) {

                        mMediaPlayer = MediaPlayer.create(this(or 
getBaseContext()),
R.raw.online);

            mMediaPlayer.start();


    }
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. need a example for TabActivity,TabHost

I want to use TabActivity,but there's no demo or snippet can be
referenced.
Who can give me a example for this.
Thanks
--~--~---------~--~----~------------~-------~--~----~

2. Some questions about ApiDemo.java

1. Basically the ApiDemo activity just calls itself recursively, is it
counter-intuitive to efficient running on mobile device?

2. The following code in getData():
        PackageManager pm = getPackageManager();
        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent,
0);

        if (null == list)
            return myData;
...
Can it be moved to somewhere for efficency purpose? Since it gets
called again and again but just gets same results.

3. Consider the code in getDate():
        Map<String, Boolean> entries = new HashMap<String, Boolean>();
...
Why use Hashmap? isn't it overskilled?

Again, since it is the demo code provided by Google, I think it is
worthy to study it in detail.
--~--~---------~--~----~------------~-------~--~----~

3. Some questions about intents and Notepad example

4. Good to join your group 鈥?A t hought : work hard, pay your taxes and smile

5. Notepadv2 - please update the tutorials

6. 2 xml files in one java file

7. BUG: terrible one