-1

basically my app has 2 activities.Say "A" and "B" . A launches B.

Activity B plays music(in a service) and also has a notification.

when the view is on activity B and i press back button ,and then i click on the the notification, activity B is opened with the state of activity completely lost and music also stops(not desired)......i am making a guess that it happens because when i press the back button the activity is explicitly destroyed ,so i have to progamatically restore its view right??

first thing that comes into mind is using onSavedInstanceState

but that won't help because onSavedInstanceState is not called when user presses back button (i read it in the docs) so what are my alternatives??

Community
  • 1
  • 1
Ankit Srivastava
  • 354
  • 1
  • 5
  • 24
  • @Raghunandan my problem is not yet solved... – Ankit Srivastava Nov 14 '13 at 20:55
  • hmmm onSaveInstanstState is best used for maintaing flags very small data for UI, For your music stuff try to play music from a background service and not UI – Pulkit Sethi Nov 14 '13 at 22:06
  • @PulkitSethi that is what i m doing but for the simplicity of question i didn't mention it here....... – Ankit Srivastava Nov 14 '13 at 22:13
  • @PulkitSethi by the way don't u sleep?its almost 4... :) – Ankit Srivastava Nov 14 '13 at 22:14
  • 1
    @user2993677 good luck to solve your problem. You say Activity B plays music and also has a notification. So whereis the service used. I don't understand. – Raghunandan Nov 15 '13 at 02:09
  • override onBackPressed (), and save data to some persistence storage like temp preferences, should be quick enough for small data – Pulkit Sethi Nov 15 '13 at 05:38
  • @Raghunandan activity B doesn't play music,it launches a service to play music......i thought there was no need to mention it i was not having problems with the service......the real question is related to saving and restoring service,doesn't matter what activity is doing what.....so have i made myself clear to you now?? – Ankit Srivastava Nov 15 '13 at 10:48
  • @Raghunandan stop wasting other people's time by asking useless details when you know you can't answer the question.... – Ankit Srivastava Nov 16 '13 at 16:02
  • @user2993677 fine don't post good luck. you have not understood my previous comments and you say i can't answer your question. No problem. Find and answer yourself or wait till some one gives your perfect and proper answer. – Raghunandan Nov 16 '13 at 16:04
  • @Raghunandan the real question is not at allrelated to which activity is doing what..and you didn't understand the comment i posted before – Ankit Srivastava Nov 16 '13 at 16:07
  • @user2993677 fine. i don't understand and i can't give you an answer. So don't bother to comment further. Good luck in solving your problem. – Raghunandan Nov 16 '13 at 16:08
  • thank you but i already got the answer – Ankit Srivastava Nov 16 '13 at 16:09

2 Answers2

1

The easiest way is to save data in onPause() and restore it in onResume(). Next question is where to store data. The are few way to do that:

  1. Using custom application context

You can extend android.app.Application and register your class in your manifest file by

<application android:name="your.application.class" ...

This will allow you to get singleton instance of your class by calling `Context.getApplicationContext().

For an example you can create

public class MyContext extends Application {
Bundle mySavedData = null;

public void setSavedData(Bundle data){
    mySavedData = data;
}

public Bundle getSavedData() {
    return mySavedData;
}
}

and then use it like this

@overide
public void onResume(){
    ...
    Bundle state = ((MyContext) getApplicationContext()).getSavedData();
    if(state != null) {
        /* restore states */
    }
    ...
}

@overide
public void onPause(){
    ...
    Bundle state = new Bundle();
    ...
    /* save your data here and save it into the context or set null otherwise*/
    ((MyContext) getApplicationContext()).setSavedData(state);
    ...
}
  1. Using singleton pattern

Instead of defining context you can create singleton instance

public class MySingleton {
    static MySingleton instance;

    public static MySingleton getInstance() {
        if(instance == null){
            instance = new MySingleton();
        } 
        return instance;
    }

    public Bundle mySavedData = null;
    void setSavedData(Bundle data){
        mySavedData = data;
    }

    public Bundle getSavedData() {
        return mySavedData;
    }
}

and you can use it

@overide
public void onResume(){
    ...
    Bundle state = MySingleton.getInstance().getSavedData();
    if(state != null) {
        /* restore states */
    }
    ...
}

@overide
public void onPause(){
    ...
    Bundle state = new Bundle();
    ...
    /* save your data here and save it into the context or set null otherwise*/
    MySingleton.getInstance().setSavedData(state);
    ...
}

Be aware that context and singletons are destroyed if application is killed. If you want to store your data permanently than using application document folder or database if recommend but I think that isn't what you looking for.

I hope this will help you....

XeNoN
  • 694
  • 6
  • 16
  • thank you...i will use your answer tomorrow and will let you know if it helps,because its 3 at night here....i think i will be reading and researching on it right now... – Ankit Srivastava Nov 14 '13 at 21:43
  • i was thinking if i should call the destroy method on pressing the back button??so that onDestroy is called and hence OnSavedInstances is called and this will reduce the amount of work i will have to do...do you think this is a good idea? – Ankit Srivastava Nov 14 '13 at 21:52
  • Take a look on [activity lifecycle](http://developer.android.com/reference/android/app/Activity.html). Application may be killed before `onDestroy()` is called, but at that point context and singleton are lost... So, I think that will probably do a job... You can use `onDestroy()`\ `onCreate()` combination. – XeNoN Nov 14 '13 at 22:03
  • i didn't get you.... will i be able to use savedinstances( i somehow find using saved instances very easy that is the reason i want to use it..) but i think it won't help because if i explicitly destroy the activity then ondestroy wont be called and hence onsavedinstancestate wont be called as well,i dunno why think there should be an easier way of restoring the activity state after pressing back button... – Ankit Srivastava Nov 14 '13 at 22:11
  • Yes, u can use them, but system will not store bundle for you, because you are starting fresh instance of activity. You must store `Bundle` by yourself, and for that I recommend using singleton. If you do not use saved instances, u can store your data in preferences just like @user2993972 said... – XeNoN Nov 14 '13 at 22:28
  • can you please explain the registration part?in xml??do we need to register contexts in manifest?? – Ankit Srivastava Nov 14 '13 at 22:57
  • and by android:name="your.application.class" ... do u mean android:name="MyContext.class" in this case? – Ankit Srivastava Nov 14 '13 at 22:59
  • my app crashes upon playing music..... – Ankit Srivastava Nov 14 '13 at 23:16
  • by android:name="your.application.class" I mean android:name="com.mydomain.myappname.maypackage.MyContext" – XeNoN Nov 15 '13 at 10:15
  • do i have to declare it in the manifest if i am using the option 2??i.e Singleton class???... my app is crashing after using singleton ...(i didn't declare it iin the manifest..) – Ankit Srivastava Nov 15 '13 at 10:54
  • Second way is easier and you don't need to declare it inside manifest. Singletons are created after `getInstacne()` first call.... – XeNoN Nov 15 '13 at 10:59
  • but my app is crashing when i use your code.... – Ankit Srivastava Nov 15 '13 at 11:01
  • ???????????................... – Ankit Srivastava Nov 15 '13 at 11:14
  • Sorry, but I cannot help you with that. Something else is causing the problem. Use LogCat to find out what.... – XeNoN Nov 15 '13 at 11:58
  • well i haven't got your code working but atleast now i know where to begin nd thanx to u – Ankit Srivastava Nov 16 '13 at 16:08
  • i still haven't understood how to declare in the manifest..... the manifest already has application tag in it..should i create a new one or should i add another tag in it? – Ankit Srivastava Nov 16 '13 at 16:27
1

As you wrote onSavedInstanceState won't help because your activity is destroyed and recreated. If you need to save the state of activity in the app lifecycle, you will need to store the data externally- for example in data base. For small data you can use shared preference.

  • i am not sure that this is what the music player apps generally do when we press back button... – Ankit Srivastava Nov 14 '13 at 21:44
  • i was thinking if i should call the destroy method on pressing the back button??so that onDestroy is called and hence OnSavedInstances is called and this will reduce the amount of work i will have to do...do you think this is a good idea? – Ankit Srivastava Nov 14 '13 at 21:52
  • It won't help you because when you will return to the activity the onCreate() will call with null savedInstanceState. This is because the activity destroyed... – user2993972 Nov 14 '13 at 21:55
  • i don't think so...if that is the case then what is the use of savedinstances ?? – Ankit Srivastava Nov 14 '13 at 22:03
  • maybe u r right...because in this case again i m explicitly calling the destruction of the activity – Ankit Srivastava Nov 14 '13 at 22:06
  • Its more because when you are clicking the notification you open a fresh instance of activity B- its not recreated. – user2993972 Nov 14 '13 at 22:09
  • no i have tried to change the settings in the manifest to single instance ...then too the UI vanishes and musicstops..that means activity is being recreated(i guess) – Ankit Srivastava Nov 14 '13 at 22:16