我有一個活動(RecoveryActivity),它有一個片段(RecoveryFragment)。點擊按鈕會調用顯示RecoveryFragment的此活動。我只是想直接調用RecoveryFragment作爲一項活動。如何將這個片段轉換成一個活動。 Noob在這裏!任何幫助將深表感謝。將片段轉換爲活動
RecoveryActivity
public class RecoveryActivity extends ActivityBase {
private static final String TAG = "password_recovery_activity";
Toolbar mToolbar;
Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recovery);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
if (savedInstanceState != null) {
fragment = getSupportFragmentManager().getFragment(savedInstanceState, "currentFragment");
} else {
fragment = new RecoveryFragment();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container_body, fragment).commit();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "currentFragment", fragment);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
fragment.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onBackPressed(){
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home: {
finish();
return true;
}
default: {
return super.onOptionsItemSelected(item);
}
}}}
RecoveryFragment
public class RecoveryFragment extends Fragment implements Constants {
private ProgressDialog pDialog;
Button mContinueBtn;
EditText mEmail;
String email;
private Boolean loading = false;
public RecoveryFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
initpDialog();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_recovery, container, false);
if (loading) {
showpDialog();
}
mEmail = (EditText) rootView.findViewById(R.id.PasswordRecoveryEmail);
mContinueBtn = (Button) rootView.findViewById(R.id.PasswordRecoveryBtn);
mContinueBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
email = mEmail.getText().toString();
if (!App.getInstance().isConnected()) {
Toast.makeText(getActivity(), R.string.msg_network_error, Toast.LENGTH_SHORT).show();
} else {
Helper helper = new Helper();
if (helper.isValidEmail(email)) {
recovery();
} else {
Toast.makeText(getActivity(), getText(R.string.error_email), Toast.LENGTH_SHORT).show();
}
}
}
});
// Inflate the layout for this fragment
return rootView;
}
public void onDestroyView() {
super.onDestroyView();
hidepDialog();
}
protected void initpDialog() {
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage(getString(R.string.msg_loading));
pDialog.setCancelable(false);
}
protected void showpDialog() {
if (!pDialog.isShowing()) pDialog.show();
}
protected void hidepDialog() {
if (pDialog.isShowing()) pDialog.dismiss();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
public void recovery() {
loading = true;
showpDialog();
CustomRequest jsonReq = new CustomRequest(Request.Method.POST, METHOD_ACCOUNT_RECOVERY, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
if (!response.getBoolean("error")) {
Toast.makeText(getActivity(), getText(R.string.msg_password_reset_link_sent), Toast.LENGTH_SHORT).show();
getActivity().finish();
} else {
Toast.makeText(getActivity(), getText(R.string.msg_no_such_user_in_bd), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
} finally {
loading = false;
hidepDialog();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading = false;
hidepDialog();
Toast.makeText(getActivity(), getText(R.string.error_data_loading), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("clientId", CLIENT_ID);
params.put("email", email);
return params;
}
};
App.getInstance().addToRequestQueue(jsonReq);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}}
爲什麼要將片段轉換爲活動?使用Fragment,這是一個很好的做法。但是您可以直接將fragment_recovery佈局添加到您的活動中。使用標記直接在container_body佈局中包含佈局 –
USKMobility