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. android:password first letter problem

Hi all,
I have the following problem:
I have an EditText with android:password="true". When I type in, the
first letter allways appears instead of a dot. I have in the xml
file:

<EditText android:id="@+id/mrs_login_user_psw_1_field"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLength="20"
        android:singleLine="True"
        android:password="true"/>

What am I doing wrong?
Thanx,
George
--~--~---------~--~----~------------~-------~--~----~

2. Problem installing .apk

Hello,

I have developed a few application for android
but have problem with one of them.
After installing my app on emulator, I can not
see it on applications' list -- I do not have problem
with my other application, only with this one.
What I do wrong?

I ran emulator:

C:\android_sdk_windows_m3-rc37a\tools\emulator

After that, I executed "adb logcat" and
installed application: adb install C:\MyApp.apk

It is possible to see file in shell:
# ls /data/app
-rw-rw-rw- root rot 70480 2008-01-20 21:40 MyApp.apk

Logcat has reported:

D/PackageManager(  467): Removing package com.mydomain.android.myapp
D/PackageManager(  467):   Services:
com.mydomain.android.myapp.LocalService
D/PackageManager(  467):   Activities: com.mydomain.android.myapp
.MyApp com.mydomain.android.myapp.Menu
com.mydomain.android.myapp.Settings com.mydomain.android.myapp.Alarm

D/ActivityManager(  467): Force removing app null
(com.mydomain.android.myapp)
D/PackageParser(  467): Scanning package: /data/app/MyApp.apk
D/PackageManager(  467): Adding package com.mydomain.android.myapp
D/PackageManager(  467):   Services:
com.mydomain.android.myapp.LocalService
D/PackageManager(  467):   Activities: com.mydomain.android.myapp
.MyApp com.mydomain.android.myapp.Menu
com.mydomain.android.myapp.Settings com.mydomain.android.myapp.Alarm


Thanks in advance for help.

Robbo
--~--~---------~--~----~------------~-------~--~----~

3. cannot get attribute from XMLReader (SAX)

4. OHA members are inside Android - literally ...njoy

5. Date and time pickers in an XML layout file - how to get to work?

6. GTALK CONNECTION PROBLEM

7. Single .apk from Multiple projects