I have a sliding menu and an action bar in my Android app. At the top of the sliding menu there is a user name and a user picture If I set them once, they are lost when I close and open the menu again.
So every time its opened Im calling a user details downloader class and Im setting the name and the avatar again, which is very irritating.
How can I set them once and dont bother with this until the app is closed, no matter whether the sliding menu is opened or closed?
public class AsdActionBarAndSlidingMenu extends AsdActionBar implements IOnUserDetailsAndStatsReceivedListener{
private TextView tvSlidingMenuUserName;
private Typeface font2;
private UserDetailsAndStatsDownloader mUserDetailsDownloader;
private String userName;
private ImageView ivSlidingMenuUserAvatar;
private String avatarPath;
private Bitmap ivSlidingMenuUserBitmap;
private static final String APP_SHARED_PREFS = "asdasd_prefs";
SharedPreferences sharedPrefs;
public Editor editor;
protected int currentlyLoggedInUser;
protected String currentlyLoggedInUserString;
public AsdActionBarAndSlidingMenu(int titleRes) {
super(R.string.app_name);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setBehindContentView(R.layout.menu_frame);
sharedPrefs = getApplicationContext().getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
currentlyLoggedInUser = sharedPrefs.getInt("currentLoggedInUserId", 0);
currentlyLoggedInUserString = Integer.toString(currentlyLoggedInUser);
tvSlidingMenuUserName = (TextView) findViewById(R.id.tvSlidingMenuUserName);
tvSlidingMenuUserName.setTypeface(font2);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame, new AsdSlidingMenuListFragment()).commit();
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setSlidingActionBarEnabled(true);
getSlidingMenu().setOnOpenedListener(new OnOpenedListener() {
@Override
public void onOpened() {
mUserDetailsDownloader = new UserDetailsAndStatsDownloader(currentlyLoggedInUserString, AsdActionBarAndSlidingMenu.this, AsdActionBarAndSlidingMenu.this);
mUserDetailsDownloader.downloadUserDetailsAndStats();
}
});
}
@Override
public void onUserDetailsAndStatsReceivedListener(UserDetailsAndStats userDetailsAndStats) {
userName = userDetailsAndStats.getUserName();
tvSlidingMenuUserName = (TextView) findViewById(R.id.tvSlidingMenuUserName);
tvSlidingMenuUserName.setText(userName);
avatarPath = userDetailsAndStats.getUserAvatar();
ivSlidingMenuUserBitmap = BitmapFactory.decodeFile(avatarPath);
ivSlidingMenuUserAvatar = (ImageView) findViewById(R.id.ivSlidingMenuUserAvatar);
ivSlidingMenuUserAvatar.setImageBitmap(ivSlidingMenuUserBitmap);
}
}
But, what gets unset is the
BitMap
, or theViews
(ivSlidingMenuUserAvatar
andtvSlidingMenuUserName
)?I dont know how you created
UserDetailsAndStatsDownloader
, but probablyonUserDetailsAndStatsReceivedListener
is called in a diferent thread. That could cause that when that thread is not running, and those views are unused, you can lose them. But im not sure.Anyways, try to inflating the views in you onCreate, and also retrieving the data after that
and let the listener just like this
then, remove
getSlidingMenu().setOnOpenedListener(...)
and lets see what happend.Besides, you should use any cache method for your downloads, so even if you need to download again file, if you have already done, no network operation is involved. For example you can do it like is shown in android-imagedownloader that is a really easy example.
If we have the code of the rest of the classes involved (just the ones you writed) the help could be much more accurate.