2016-07-18 87 views
0

我有一個活動(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

爲什麼要將片段轉換爲活動?使用Fragment,這是一個很好的做法。但是您可以直接將fragment_recovery佈局添加到您的活動中。使用標記直接在container_body佈局中包含佈局 – USKMobility

回答

0

可通過擴展而不是片段AppCompatActivity開始

public class RecoveryActivity extends AppCompatActivity(這是你的片段,但我給它改名RecoveryActivity。你不應該因爲你'd有兩個同名的活動。我們說它叫做RecoveryActivityNew。下面的說明不適合你命名爲RecoveryActivity,但是用於RecoveryFragment的一段代碼。

然後,您刪除僅與片段相關的onAttach()和onDetach()等方法。擺脫onDestroyView。刪除onCreateView()和所有在那裏的代碼,把它的onCreate()

現在,因爲你沒有一個片段,你引用一個觀點是這樣的:

mContinueBtn = (Button) findViewById(R.id.PasswordRecoveryBtn);

剛就像你在活動中引用工具欄一樣(你失去了rootView)。

此外,在顯示吐司例如,您不使用getActivity()方法,但使用thisRecoveryActivity.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> 
+0

是的,我試着按照你所說的去做這樣的事情,但是我想我會犯一些錯誤。我也在清單中宣佈它。如果你能告訴我如何在上面的代碼中做到這一點,這將是非常有幫助的。學習機器人並沒有得到太多你上面所說的。我會感激 –