Back to project page FragmentTutorial.
The source code is released under:
Apache License
If you think the Android project FragmentTutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yanlu.android.fragment.frg; /*w w w .j a v a 2 s.c o m*/ import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import com.yanlu.android.fragment.MainActivity; import com.yanlu.android.fragment.R; import com.yanlu.android.fragment.SettingsActivity; import com.yanlu.android.fragment.model.DemoParcel; public class RightFragment extends Fragment implements View.OnClickListener { private static final String TAG = "RightFragment"; // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private DemoParcel mParam2; private TextView mTv; /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment LeftFragment. */ // TODO: Rename and change types and number of parameters public static RightFragment newInstance(String param1, DemoParcel param2) { RightFragment fragment = new RightFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putParcelable(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } public RightFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getParcelable(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "onCreateView(LayoutInflater, ViewGroup, Bundle) "); setRetainInstance(true); // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_right, container, false); view.findViewById(R.id.btn_ok).setOnClickListener(this); mTv = (TextView) view.findViewById(R.id.content); if (TextUtils.isEmpty(mParam1) || null == mParam2) { mTv.setText("no param"); } else { mTv.setText(ARG_PARAM1 + " = " + mParam1 + "\r\n" + ARG_PARAM2 + " = " + mParam2.getName() + "\r\ngetActivity(): " + ((MainActivity)getActivity()).getStringFromActivity()); } return view; } @Override public void onResume() { super.onResume(); Log.d(TAG, "onResume( )"); } @Override public void onAttach(Activity activity) { super.onAttach(activity); Log.d(TAG, "onAttach(activity)"); } @Override public void onDetach() { super.onDetach(); Log.d(TAG, "onDetach( )"); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.d(TAG, "onActivityCreated(savedInstanceState)"); } @Override public void onStart() { super.onStart(); Log.d(TAG, "onStart( )"); } @Override public void onPause() { super.onPause(); Log.d(TAG, "onPause( )"); } @Override public void onStop() { super.onStop(); Log.d(TAG, "onStop( )"); } @Override public void onDestroyView() { super.onDestroyView(); Log.d(TAG, "onDestroyView( )"); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy( )"); } @Override public void onClick(View v) { //Toast.makeText(getActivity(), "onClick() in fragment", Toast.LENGTH_SHORT).show(); startActivityForResult(new Intent(getActivity(), SettingsActivity.class), REQ_CODE); } private final static int REQ_CODE = 22223; @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (REQ_CODE == requestCode && resultCode == Activity.RESULT_OK) { Toast.makeText(getActivity(), "onActivityResult() in fragment", Toast.LENGTH_SHORT).show(); } else { super.onActivityResult(requestCode, resultCode, data); } } }