从活动屏幕登录,并将其定向到片段



我正在尝试从登录活动启动片段屏幕。我希望能够登录它将检查用户名和密码,将其他数据传递到片段屏幕。到目前为止,我已经将其与活动屏幕一起使用,但是现在试图使其用于片段屏幕。但是无法让它工作,任何人都可以建议解决吗?

登录

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String username = etUsername.getText().toString();
            final String password = etPassword.getText().toString();
            Response.Listener<String> listener = new Response.Listener<String>(){
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");
                        if(success) {
                            String sname = jsonResponse.getString("sname");
                            String fname = jsonResponse.getString("fname");
                            String email = jsonResponse.getString("email");
                            String tel_no = jsonResponse.getString("phone_number");
                            String addr1 = jsonResponse.getString("address_line1");
                            String post_code = jsonResponse.getString("post_code");
                            String city = jsonResponse.getString("city");
                            String account_id = jsonResponse.getString("account_id");
                            Bundle bundle = new Bundle();
                            bundle.putString("surname", sname);
                            bundle.putString("forename", fname);
                            bundle.putString("email", email);
                            bundle.putString("tel_no", tel_no);
                            bundle.putString("address_1", addr1);
                            bundle.putString("city", city);
                            bundle.putString("post_code", post_code);
                            bundle.putString("account_id", account_id);
                            FragmentManager fm = getFragmentManager();
                            FragmentTransaction ft = fm.beginTransaction();
                            BookingFragment mFragment = BookingFragment.newInstance(user);
                            ft.replace(R.id.container, mFragment);
                            ft.setArguments(bundle)
                            ft.commit();
                        }
                        else {
                            String failure = jsonResponse.getString("failure");
                            AlertDialog.Builder  builder = new AlertDialog.Builder(LoginActivity.this);
                            builder.setMessage("Login failed: "+failure)
                                    .setNegativeButton("Return", null)
                                    .create()
                                    .show();
                        }
                    }
                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };
            LoginRequest loginRequest = new LoginRequest(username, password, listener);
            RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
            queue.add(loginRequest);
        }
    });

预订范围

public class BookingFragment extends Fragment{
    public static final String USER = "user";
    public static BookingFragment new Instance(User user){
        Bundle bdl = new Bundle();
        bdl.putExtra(USER , user);
        BookingFragment fragment = new BookingFragment();
        fragment.setArgument(bdl);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
            container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.booking_fragment, container,
                false);
        return view;
    }
}
inside on onResponse change your code , also convert response in the model
FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    BookingFragment mFragment = BooingFragment.newInstance(user);
    ft.replace("your fragment container resource id", mFragment);
    ft.commit();
**Fragment class**
public class BookingFragment extends Fragment{
    public static final String USER = "user";
    public static BookingFragment new Instance(User user){
      Bundle bdl = new Bundle();
      bdl.putExtra(USER , user);
      Booking fragment = new BookingFragment();
      fragment.setArgument(bdl);
      return fragment;
     }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
      container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.booking_fragment, container, 
        false);
        return view;
    }
}

无法使用意图开始,必须通过片段管理器启动它们。这是一些从这里启发的示例代码启动片段

Fragment fragment = new BookingFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_place, fragment);
fragmentTransaction.commit();

您还需要在布局中使用ID fragment_place或类似内容具有片段XML元素。您似乎还通过了意图。由于您无法使用意图,因此无法使用意图。相反,您可以创建一个捆绑包并将捆绑包作为片段的参数

Bundle bundle = new Bundle()
bundle.putString("key","value")
fr.setArguments(bundle)

然后,您可以从片段中拨打getArguments().getString("key");,以获取您刚刚传递的值。