我有一个活动(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();
}}
答案 0 :(得分:0)
首先扩展AppCompatActivity而不是Fragment
public class RecoveryActivity extends AppCompatActivity
(这是你的片段,但我将其重命名为RecoveryActivity。你不应该让你有2个同名的活动。让我们说它叫做RecoveryActivityNew。下面的说明不是你命名的作为RecoveryActivity但是对于RecoveryFragment代码段。
然后删除仅与片段相关的onAttach()和onDetach()等方法。摆脱onDestroyView。删除onCreateView()及其中的所有代码,将其放入onCreate()
现在因为你没有片段,所以引用这样的视图:
mContinueBtn = (Button) findViewById(R.id.PasswordRecoveryBtn);
就像你引用了活动中的工具栏一样(你丢失了rootView)。
此外,在显示Toast时,您不使用getActivity()方法,而是使用this
或RecoveryActivity.this
:
Toast.makeText(RecoveryActivity.this, R.string.msg_network_error, Toast.LENGTH_SHORT).show();
摆脱构造函数public RecoveryFragment()
不要忘记在应用的清单文件中声明您的新活动。
<activity
android:name=".RecoveryActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.your.packagename.RecoveryActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>