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. How secure is SharedPreferences?

We want to store credentials for a user to a web service so the user
doesn't have to repeatedly login, but we're concerned about security.
We can't store a hash on the database, but we could probably use JCE
encryption locally.

Is the content in SharedPreferences secured on the Android device?

Brian
--~--~---------~--~----~------------~-------~--~----~

2. Simulate speech on emulator?

Hi,

My application records the audio data coming from Microphone, saves it
to an AMR file and then sends it to the server for results.

Is there a way I can change the AudioSource from microphone to some
audio file? This way I would be able to simulate the speech
functionality. OR in some way if I can feed audio into the mike it
will solve my purpose.

Thanks
Yasser
--~--~---------~--~----~------------~-------~--~----~

3. SoundPool.stop(streamId) intermittently never returns (freezes) on Android 1.5 on G1

4. about Intent.createChooser

5. for adc2 only: help the most

6. augmented reality frustum

7. XML Menu