活动之间的沟通,而不是启动第二个活动

时间:2017-06-21 08:00:34

标签: android android-fragments android-activity

我有一个包含复选框的片段,此片段与活动configActivity相关。我能够检索活动中复选框的状态。 我想在启动应用程序时从另一个活动(主活动)中检索此状态。 我已经尝试过这种方法,但是使用OnActivityResult我得到一个nullpointer,我必须启动其他活动才能工作。

这是我的片段:

public class configFragment extends android.support.v4.app.Fragment {
CheckBox check;
configActivity cmt;

OnChecked mCallback;



public interface OnChecked {
     void Oncheck(Boolean checkstatus);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.config_layout, container, false);
    check = (CheckBox) rootView.findViewById(R.id.checkbox1);
    TextView text1=(TextView) rootView.findViewById(R.id.text1);
    text1.setText("hello ");

   // cmt=(configActivity) getActivity();
    check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(check.isChecked()){
                mCallback.Oncheck(true);

            }else{
                Log.d("status","not checked");
              mCallback.Oncheck(false);
            }
        }
    });

    Boolean test=load();
    if(test){
        mCallback.Oncheck(true);
    }else{
        mCallback.Oncheck(false);
    }


    return rootView;
}


@Override
public void onPause() {
    super.onPause();
    save(check.isChecked());
}

@Override
public void onResume() {
    super.onResume();
    check.setChecked(load());

    Boolean test=load();
    if(test){
        mCallback.Oncheck(true);
    }else{
        mCallback.Oncheck(false);
    }
}

private void save(final boolean isChecked) {
    SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("check", isChecked);
    editor.commit();
}

public boolean load() {
    SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
    return sharedPreferences.getBoolean("check", check.isChecked());
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (OnChecked) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnChecked");
    }
}

这是我的班级:

public class configActivity extends SingleFragmentActivity implements configFragment.OnChecked {

public Boolean checking;

@Override
protected Fragment createFragment() {
    return new configFragment();
}




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // configFragment cf=new configFragment();





}


@Override
public void Oncheck(Boolean checkstatus) {
    if(checkstatus){
        checking=true;
        Log.d("status of checking",checking.toString());
    }else{
        checking=false;
        Log.d("status of checking",checking.toString());
    }
}

@Override
protected void onResume() {
    super.onResume();
    Intent intent=new Intent();
    intent.putExtra("value",checking);
    setResult(2,intent);

}
}

这是我的主要活动,我想要检索复选框的状态:

public class MainActivity extends AppCompatActivity {

public Boolean check;
public Boolean message;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView hello=(TextView) findViewById(R.id.hello);
    Button but1=(Button) findViewById(R.id.button1);


    but1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(MainActivity.this,configActivity.class);
            startActivity(i);
            startActivityForResult(i,2);



        }
    });

    hello.setText("Hello world");



    if(message){
        Log.d("value of message",message.toString());
    }else{
        Log.d("value of message",message.toString());
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // check if the request code is same as what is passed  here it is 2
    if(requestCode==2)
    {
         message=data.getExtras().getBoolean("value");

    }
}
}

2 个答案:

答案 0 :(得分:1)

使用共享首选项存储复选框值,然后您可以随时检索它而无需再次启动配置活动。

您可以存储复选框值,如下所示:

// cmt=(configActivity) getActivity();
check.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        SharedPreferences sharedPref = getSharedPreferences("settings", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("checkboxValue", check.isChecked());
        editor.commit();
    }
});

然后您可以随时在MainActivity中检索它:

SharedPreferences sharedPref = getSharedPreferences("settings", Context.MODE_PRIVATE);
boolean defaultValue = getResources().getBoolean("checkboxValue", false);

答案 1 :(得分:0)

您是否考虑过使用interfaces

Communicating between Fragments and Activities

我们的想法是,您的活动实现了一个界面(例如onRadioChecked()),一旦用户选中了复选框以及您想要的任何内容,您就可以在活动中检测到该事件。

要处理活动更改之间的UI状态,请阅读上面的链接。