1

I'm still facing with a favorite button which should save my layout and set the layout as the layout of a new activity .. The problem is that my activity which should open the layout I saved, doesn't do that, but I don't understand why.. Here is my code:

public void AddToFavoriteListener(final View v){
        SharedPreferences prefs=getSharedPreferences("MYPREF",MODE_PRIVATE);
        int favActivity=prefs.getInt("FAV_ACTIVITY", 0);

        switch(favActivity){
        case 0:
            Toast.makeText(getApplication(),"Favorite not set.",Toast.LENGTH_SHORT).show();
            break;
        case 1:
            //code to open first activity
            break;
        case 2:
            //code to open second activity
            break;
        }
        return;

And here is the code for saving the layout:

public void AddToFavorite(View v){    
            SharedPreferences pref=getSharedPreferences("MYPREF",MODE_PRIVATE);
            SharedPreferences.Editor editor=pref.edit();
            int activityNum = 0;
            editor.putInt("FAV_ACTIVITY", activityNum);
            editor.commit();
        }

Thanks a lot !

L.E: I edited the first code for the activity which should be open the saved layout, but the button still opens a black activity..

Ezekiel
  • 89
  • 3
  • 12
  • I don't understand exactly what the problem is. Is it that your preferences are not being saved, or that you don't know how to dynamically open an activity based on what the preferences are? – Curtis Shipley Feb 02 '14 at 21:13
  • The 2nd is my problem.. I don't know how to open what I saved, or it does not open what I saved.. when I open the activity which contains the first code, the activity which is opened is blank, just a black background and that's all.. – Ezekiel Feb 02 '14 at 21:31

1 Answers1

0

Assuming that once the preferences are saved, the next time the user opens the app you want it to launch a different activity..

The easiest way to do this would be to get the preference from within the onCreate() then open the correct activity. For example, call this from the onCreate():

private void openFromFavorites() {
        SharedPreferences prefs=getSharedPreferences("MYPREF",MODE_PRIVATE);
        int favActivity=prefs.getInt("FAV_ACTIVITY", 0);

        switch(favActivity){
        case 0:
            Toast.makeText(getApplication(),"Favorite not set.",Toast.LENGTH_SHORT).show();
            break;
        case 1:
            Intent intent = new Intent(this, ActivityForPref1.class );
            startActivity(intent);
            finish();
            break;
        case 2:
            Intent intent = new Intent(this, ActivityForPref2.class );
            startActivity(intent);
            finish();
            break;
        }
}

If you are already doing this, but the new activity just starts blank, then it probably has to do with the way you're calling setContentView().

Curtis Shipley
  • 7,990
  • 1
  • 19
  • 28
  • Still the same black background.. damn.. can't you review the code which saves the layout to favorite..? Maybe there's a problem too.. Or... if you wanted to creat an app like that, with a favorite button, which saves the current layout to be opened by another button .. how would you do that..? Do you think that the codes might be not right..? can u check please? Thanks a lot. – Ezekiel Feb 02 '14 at 21:50
  • Sure, either post the code here or let me know if you have in it a public repo. My contact info is in my profile if you need it. – Curtis Shipley Feb 02 '14 at 21:53
  • The codes are in the first post. The first code, is the code that opens that layout that the 2nd code save the layout.. do you think that's ok? – Ezekiel Feb 02 '14 at 21:55
  • A few things. First, there isn't any code there that actually opens the activities so I can't see what you're doing. Also, I don't see the onCreate of the other activities so I can't tell if you're opening the layouts correctly, third I don't see how those two methods are being called. So there isn't enough information to know what the problem is. Do you have the complete project posted somewhere? – Curtis Shipley Feb 02 '14 at 22:00
  • I'm sorry for the way I uploaded it, but still taking some time to uploade it on github.. here is the link: girlshare.ro/33238334.8 .. The activity that cointans the layout I want to save is called DoispeA, and Favorite is the activity which should be open the saved layout. Thanks a lot ! – Ezekiel Feb 02 '14 at 22:20
  • Ok, I got the code and think I understand what the problem is. Is there some way we can chat? Send me an email spurious.thought@gmail.com with your skype id.. – Curtis Shipley Feb 02 '14 at 22:38