将片段按钮链接到另一个活动

时间:2016-04-25 07:11:56

标签: android android-fragments android-studio

以下是已编辑的代码

@Override
public void onClick(View v) {
    switch (v.getId()){

        case R.id.btn_chg_password:
            showDialog();
            break;
        case R.id.btn_order:
            changemenu();
            break;
    }
}

private void changemenu() {
    SharedPreferences.Editor editor = pref.edit();
    editor.putBoolean(Constants.IS_LOGGED_IN,false);
    editor.putString(Constants.EMAIL,"");
    editor.putString(Constants.NAME,"");
    editor.putString(Constants.UNIQUE_ID,"");
    editor.apply();
    order();
}

private void order(){

    Fragment login = new LoginFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.activity_activityresult1,activityresult1);
    ft.commit();

我想在按 btn_order 之后更改指向activity_activityresult1的链接我相信我不能像这样链接它,因为它的片段是活动的吗?单击 btn_order 我想直接转到 activity_activityresult1 < -----此布局

以下是原始代码

 @Override
public void onClick(View v) {
    switch (v.getId()){

        case R.id.btn_chg_password:
            showDialog();
            break;
        case R.id.btn_logout:
            logout();
            break;
    }
}

private void logout() {
    SharedPreferences.Editor editor = pref.edit();
    editor.putBoolean(Constants.IS_LOGGED_IN,false);
    editor.putString(Constants.EMAIL,"");
    editor.putString(Constants.NAME,"");
    editor.putString(Constants.UNIQUE_ID,"");
    editor.apply();
    goToLogin();
}

private void goToLogin(){

    Fragment login = new LoginFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_frame,login);
    ft.commit();
}

1 个答案:

答案 0 :(得分:0)

private void order(){
  Intent mIntent = new Intent();
  mIntent.setClassName(your_package_name_string, your_activity_name_string);
  // for ex: your package name can be "com.example"
  // your activity name will be "com.example.Contact_Developer"
  startActivity(myIntent);

  or 
 Intent myIntent = new Intent(getActivity(), TargetActivity.class);
 startActivity(myIntent);
 }

如果您想从“订单”按钮导航到活动,请单击片段希望这会有所帮助。