如何从 android 中的片段开始一个 activity?
How to start an activity from fragment in android?
我想要一个 activity 并在单击片段按钮将 activity 更改为第二个 activity
时使用片段
示例:在登录中 activity 我单击注册...如果创建用户更改 activity 为主并完成登录 activity...
这是我的片段代码:
View view = inflater.inflate(R.layout.fragment_register, container, false);
edUsername = view.findViewById(R.id.edUser);
edUserEmail = view.findViewById(R.id.edEmail);
edUserPhone = view.findViewById(R.id.edPass);
edUserPassword = view.findViewById(R.id.edPassword);
btnLogin = view.findViewById(R.id.btnLogin);
btnRegister = view.findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog loader = ProgressDialog.show(getContext(),
"Inserting...", "please wait",
false, false);
StringRequest request = new StringRequest(Request.Method.POST, Config.register_api,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("a user with same information existing")) {
loader.dismiss();
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
}else {
loader.dismiss();
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
loader.dismiss();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("username", edUsername.getText().toString().trim());
params.put("userphone", edUserPhone.getText().toString().trim());
params.put("useremail", edUserEmail.getText().toString().trim());
params.put("userpassword", edUserPassword.getText().toString().trim());
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(request);
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
如果您想从 Fragment
开始新的 Activity
,您可以使用:
Intent intent = new Intent(getActivity(), NewActivity.class); // creating new intent
startActivity(); // starting new activity with intent
getActivity().finish(); // finish current activity
只需将此插入到您要开始新的代码的位置 activity。
如果你想从另一个 Activity
开始新的 Activity
使用这个:
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity();
finish();
我想要一个 activity 并在单击片段按钮将 activity 更改为第二个 activity
时使用片段示例:在登录中 activity 我单击注册...如果创建用户更改 activity 为主并完成登录 activity...
这是我的片段代码:
View view = inflater.inflate(R.layout.fragment_register, container, false);
edUsername = view.findViewById(R.id.edUser);
edUserEmail = view.findViewById(R.id.edEmail);
edUserPhone = view.findViewById(R.id.edPass);
edUserPassword = view.findViewById(R.id.edPassword);
btnLogin = view.findViewById(R.id.btnLogin);
btnRegister = view.findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog loader = ProgressDialog.show(getContext(),
"Inserting...", "please wait",
false, false);
StringRequest request = new StringRequest(Request.Method.POST, Config.register_api,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("a user with same information existing")) {
loader.dismiss();
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
}else {
loader.dismiss();
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
loader.dismiss();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("username", edUsername.getText().toString().trim());
params.put("userphone", edUserPhone.getText().toString().trim());
params.put("useremail", edUserEmail.getText().toString().trim());
params.put("userpassword", edUserPassword.getText().toString().trim());
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(request);
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
如果您想从 Fragment
开始新的 Activity
,您可以使用:
Intent intent = new Intent(getActivity(), NewActivity.class); // creating new intent
startActivity(); // starting new activity with intent
getActivity().finish(); // finish current activity
只需将此插入到您要开始新的代码的位置 activity。
如果你想从另一个 Activity
开始新的 Activity
使用这个:
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity();
finish();