Back to project page NestedFragment.
The source code is released under:
GNU General Public License
If you think the Android project NestedFragment 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.sams.nestedfragment; //from www . j a va 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.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class PagesFragment extends Fragment { private int mPosition; private String mTextviewText; private TextView mTextView; private Button mFirstActivityButton; private Button mSecondActivityButton; private final int REQUEST_FIRST = 1; private final int REQUEST_SECOND = 2; public static PagesFragment getInstance(int position) { PagesFragment fragment = new PagesFragment(); // Supply num input as an argument. Bundle args = new Bundle(); args.putInt("position",position); fragment.setArguments(args); return fragment; } public PagesFragment(){} @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPosition = getArguments() != null ? getArguments().getInt("position") : 0; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_pages, container,false); mTextView = (TextView) rootView.findViewById(R.id.tv_textview); mFirstActivityButton = (Button) rootView.findViewById(R.id.btn_firstActivity); mSecondActivityButton = (Button) rootView.findViewById(R.id.btn_secondActivity); mTextviewText = "Child Fragment "+String.valueOf(mPosition + 1); mTextView.setText(mTextviewText); Log.e("created", ""+mPosition); mFirstActivityButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), FirstActivity.class); getParentFragment().startActivityForResult(intent, REQUEST_FIRST); } }); mSecondActivityButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), SecondActivity.class); getParentFragment().startActivityForResult(intent, REQUEST_SECOND); } }); return rootView; } public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK){ if(requestCode == REQUEST_FIRST){ mTextviewText = data.getStringExtra("firstReturnValue"); mTextView.setText(mTextviewText); } else if (requestCode == REQUEST_SECOND) { mTextviewText = data.getStringExtra("secondReturnValue"); mTextView.setText(mTextviewText); } } super.onActivityResult(requestCode, resultCode, data); } }