安卓:里面tabhost共享preferences不工作 [英] Android : Shared preferences inside tabhost not working

查看:150
本文介绍了安卓:里面tabhost共享preferences不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个活动tabhost,我想保存$ P $每个活动的按钮pssed状态

所以,现在我怎么能保存在所有三个子活动pssed每个按钮的状态$ P $,这样当我移动从一个活动到另一个按钮pressed状态将搬回来体现。第一项活动 - >所有4个按钮pressed - >去第2个活动 - >回来的第一项活动 - >在第一个活动的所有按钮应该在pressed状态

当我去老二选项卡,来到第一个孩子选项卡中的变化(我pressed的按钮不在pressed状态)是不是反映了

帮助总是AP preciated,谢谢

这是第一tabhost孩子的活动我的code

  @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    sea​​tdirnbtn.setOnClickListener(listner1);
    sea​​tdirnbtn1.setOnClickListener(listner2);
        sea​​tdirnbtn.set pressed(真);
        sea​​tdirnbtn1.set pressed(真);
        this.Load preferences();       }    私人无效节省preferences(){    共享preferences共享preferences = getApplicationContext()getShared preferences(共同preferences,MODE_WORLD_READABLE)。
    共享preferences.Editor编辑=共享preferences.edit();
    editor.putBoolean(国家,seatdirnbtn.isEnabled());
    editor.putBoolean(STATE1,seatdirnbtn1.isEnabled());
    editor.commit();}私人无效负荷preferences(){
    共享preferences共享preferences = getApplicationContext()getShared preferences(共同preferences,MODE_WORLD_READABLE)。
    布尔州=共享preferences.getBoolean(国家,FALSE);
    布尔状态1 =共享preferences.getBoolean(STATE1,FALSE);
    sea​​tdirnbtn.set pressed(州);
    sea​​tdirnbtn1.set pressed(STATE1);
}         @覆盖
     保护无效调用onStart(){
    super.onStart();
        加载preferences();
        }        @覆盖
       保护无效的onPause(){
    节省preferences();
    super.onPause();
      }       公共静态布尔isclick = FALSE;
私人View.OnClickListener listner1 =新View.OnClickListener(){    公共无效的onClick(视图v){        如果(isclick){
            sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4hlt);        }其他{
            sea​​tdirnbtn.setBackgroundResource(R.drawable.icon4);
        }        !isclick = isclick;
    }};私人View.OnClickListener listner2 =新View.OnClickListener(){    公共无效的onClick(视图v){        如果(isclick){
            sea​​tdirnbtn1.setBackgroundResource(R.drawable.icon2hlt);
        }其他{
            sea​​tdirnbtn1.setBackgroundResource(R.drawable.icon2);
        }        !isclick = isclick;
    }};


解决方案

也许你应该重写你应该设置按钮状态onResume()方法。此方法的onCreate后称为()和已创建甚至活性。如果你有tabHost他们不是每次标签之间切换等等的onCreate时间创建活动()方法将被调用一次,但onResume()每次你切换到特别活动标签的时间。

您code这是加载preferences是在onStart()方法。看看这里的活动周期。你可以看到,如果你的活动是之前停止,但如果它只是暂停永远不会调用此方法只调用了。

编辑:

如果你有一个像刚刚2个状态,从您的问题code它可以更好地使用切换按钮也一般有2个状态。你可以风格它为每个国家不同的背景。 本教程可能是有益的

比你有一点点不同的监听器:

  toggleButton.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
        @覆盖
        公共无效onCheckedChanged(CompoundButton compoundButton,布尔器isChecked){            如果(选中){
                //做某事,如果它检查
            }其他{
                //做某事,如果它未被选中;
            }        }
    });

以编程方式更改状态为他们:

  toggleButton.setChecked(真); //还是假的

所以最后你可以将此状态保存到共享preferences:

  editor.putBoolean(toggleButton1,toggleButton.isChecked());

当你需要这种状态:

 布尔=器isChecked共享preferences.getBoolean(toggleButton1,FALSE);
toggleButton.setChecked(器isChecked);

选择将采取的切换按钮的背景为每个状态的照顾。

I have a tabhost with three activities and I want to save the pressed state of the buttons of each activity

So now How can I save the pressed state of each button in all three child activities so that when I move from one activity to the other the button pressed state will be reflected on moving back. first activity -> all 4 buttons pressed -> go to 2nd activity -> come back to first activity -> all buttons in first activity should be in pressed state

When I go to second child tab and come to the first child tab the change(The buttons which I pressed are not in pressed state) is not reflecting

Help is always appreciated , Thanks

this is my code in first tabhost child activity

         @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    seatdirnbtn.setOnClickListener(listner1);
    seatdirnbtn1.setOnClickListener(listner2);
        seatdirnbtn.setPressed(true);
        seatdirnbtn1.setPressed(true);
        this.LoadPreferences();

       }

    private void SavePreferences() {

    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("sharedPreferences",MODE_WORLD_READABLE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("state", seatdirnbtn.isEnabled());
    editor.putBoolean("state1", seatdirnbtn1.isEnabled());
    editor.commit();

}

private void LoadPreferences() {
    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("sharedPreferences",MODE_WORLD_READABLE);
    Boolean state = sharedPreferences.getBoolean("state", false);
    Boolean state1 = sharedPreferences.getBoolean("state1", false);
    seatdirnbtn.setPressed(state);
    seatdirnbtn1.setPressed(state1);
}

         @Override
     protected void onStart() {
    super.onStart();
        LoadPreferences();
        }

        @Override  
       protected void onPause() {
    SavePreferences();
    super.onPause();
      }

       public static boolean isclick = false;
private View.OnClickListener listner1 = new View.OnClickListener() {

    public void onClick(View v) {

        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);

        } else {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
        }

        isclick = !isclick;
    }

};

private View.OnClickListener listner2 = new View.OnClickListener() {

    public void onClick(View v) {

        if (isclick) {
            seatdirnbtn1.setBackgroundResource(R.drawable.icon2hlt);
        } else {
            seatdirnbtn1.setBackgroundResource(R.drawable.icon2);
        }

        isclick = !isclick;
    }

};

解决方案

probably you should override onResume() method in which you should set buttons states. this method is called after onCreate() and even the activity is already created. If you have activities in tabHost they are not created each time you switch between tabs so onCreate() method will be called only once but onResume() every time you switch to tab with particular activity.

your code which is loading preferences is in onStart() method. Look here on activity lifecycle. You can see that this method is called only if your activity was stopped before but will never called if it was just paused.

EDIT:

if you have just 2 states like in your code from question it could be better to use ToggleButton which also generally have 2 states. You can style it to have different backgrounds for each state. This tutorial could be helpfull.

Than you will have a little bit different Listener:

    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            if(checked) {
                //do sth if it's checked
            } else {
                //do sth if it's not checked;
            }

        }
    });

to change states for them programatically:

toggleButton.setChecked(true); //or false

so finally you can save this state to SharedPreferences:

editor.putBoolean("toggleButton1",toggleButton.isChecked());

and when you will need this state:

boolean isChecked = sharedPreferences.getBoolean("toggleButton1",false);
toggleButton.setChecked(isChecked);

selector will take care of switching button backgrounds for each state.

这篇关于安卓:里面tabhost共享preferences不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆