How to a get a view of fragment in Android - android

Hello I am using Fragment in my android application. I need to get the view which I can get using.
mNoteEditText = rootView.findViewById(R.id.noteEditText);
This mNoteEditText is require to access in onBackPressed so every view reference I need to make them static variable because of Fragment class is static. I know to make every view to static variable is not good approach. How this I can make such that I dont need to make any static variable of the view.
public class NotesActivity extends Activity {
private int bookId;
private int chapterId;
private static EditText mNoteEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
// get data from intent that sent from home activity
bookId = getIntent().getIntExtra("book_id", -1);
chapterId = getIntent().getIntExtra("book_id", -1);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new NoteFragment()).commit();
}
}
/**
* A note fragment containing a note layout.
*/
public static class NoteFragment extends Fragment {
public NoteFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notes,
container, false);
mNoteEditText = (EditText) rootView.findViewById(R.id.noteEditText);
return rootView;
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
// get database instance
MySQLiteOpenHelper db = MySQLiteOpenHelper.getInstance(this);
Notes note = new Notes();
note.setBookId(bookId);
note.setChapterId(chapterId);
note.setNote(mNoteEditText.getText().toString());
}
}
Please help and thanks in advance.

A Fragment has a Method called getView(). With it you can get the View of the Fragment as long as it is attached to an Activity.
View view = fragment.getView();
But if you are looking for a View inside the Fragment you can also just get it with findViewById() from the Activity. Again the Fragment has to be attached to the Activity for this to work.
BUT you should not do that. Nothing outside the Fragment should have anything to do with something inside the Fragment. Write public methods in the Fragment to interact with it. Try something like this:
public static class NoteFragment extends Fragment {
private EditText noteEditText;
public NoteFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notes, container, false);
this.noteEditText = (EditText) rootView.findViewById(R.id.noteEditText);
return rootView;
}
// I added the following 3 methods to interact with the Fragment
public boolean isEmpty() {
final String text = this.noteEditText.getText().toString();
return text.isEmpty();
}
public String getText() {
return this.noteEditText.getText().toString();
}
public void setText(String text) {
this.noteEditText.setText(text);
}
}
And now in your Activity you can do this:
public class NotesActivity extends Activity {
private int bookId;
private int chapterId;
private NoteFragment noteFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
// get data from intent that sent from home activity
bookId = getIntent().getIntExtra("book_id", -1);
chapterId = getIntent().getIntExtra("book_id", -1);
if (savedInstanceState == null) {
this.noteFragment = new NoteFragment();
getFragmentManager().beginTransaction().add(R.id.container, this.noteFragment).commit();
}
// Now you can interact with the Fragment
this.noteFragment.setText("some text");
...
if(!this.noteFragment.isEmpty()) {
String note = this.noteFragment.getText();
...
}
}
}

Related

Child Fragment is not animated on EnterTransition

After changing to the Android Navigation Component I encountered some problems with migrating my Activity Transitions to the corresponding Fragment Transitions.
Specifically, it appears that the EnterTransition of a Fragment doesn't apply to it's Child Fragments.
I've set up a test activity that contains an OuterTestFragment, which in turn consists of a TextView and another fragment, InnerTestFragment.
A button in the activity then replaces the OuterTestFragment with a new OuterTestFragment to see if the contents transition smoothly.
Here are the relevant classes:
Activity:
public class PlaygroundActivity extends AppCompatActivity {
public static final ArrayList<String> strings = new ArrayList<>();
static {
strings.add("sgdgdgsdfggggggggsdgsdfgsd\nsdsdgsdfgds\ndfgsd\nsdsdgsdfgds\ndfgsd");
strings.add("sgdgdgsdfgggg\nggggsndfgsd\nsdsdgsdfgds\ndfgsd");
strings.add("sgdgsd\nsdsdgsdfgds\ndfgsd");
strings.add("sgdgdgsdfggfgds\ndfgsd");
strings.add("sgdgdgsdfggggggggsdg");
}
int count = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playground);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainerOuter, OuterTestFragment.getInstance("This is an inner text", "Outer text!"))
.commit();
}
public void replace(View v) {
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.addToBackStack("replaced")
.replace(R.id.fragmentContainerOuter, InnerTestFragment.getInstance(count++ >= 4 ? "Default" : strings.get(count)))
.commit();
}
}
OuterTestFragment:
public class OuterTestFragment extends TransitionedFragment {
private static final String ARG_OUTER_TEXT = "OuterTestFragment:outerText";
private static final String ARG_INNER_TEXT = "OuterTestFragment:innerText";
private String innerText;
private String outerText;
public static OuterTestFragment getInstance(String inner, String outer) {
OuterTestFragment outerFragment = new OuterTestFragment();
Bundle args = new Bundle();
args.putString(ARG_OUTER_TEXT, outer);
args.putString(ARG_INNER_TEXT, inner);
outerFragment.setArguments(args);
return outerFragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
innerText = getArguments().getString(ARG_INNER_TEXT);
outerText = getArguments().getString(ARG_OUTER_TEXT);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.test_fragment_outer, container, false);
((TextView) layout.findViewById(R.id.textOuter)).setText(outerText);
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, InnerTestFragment.getInstance(innerText))
.commit();
return layout;
}
}
TransitionedFragment:
public abstract class TransitionedFragment extends Fragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TransitionInflater inflater = TransitionInflater.from(getContext());
setEnterTransition(inflater.inflateTransition(R.transition.default_transition));
setReenterTransition(inflater.inflateTransition(R.transition.default_transition));
setSharedElementEnterTransition(inflater.inflateTransition(R.transition.scaled_img_clip_transition));
}
}
Inner Test Fragment:
public class InnerTestFragment extends Fragment {
private static final String ARG_TEXT = "InnerTestFragment:text";
private String text;
public static InnerTestFragment getInstance(String text) {
InnerTestFragment frgmt = new InnerTestFragment();
Bundle args = new Bundle();
args.putString(ARG_TEXT, text);
frgmt.setArguments(args);
return frgmt;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = getArguments().getString(ARG_TEXT);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.test_fragment_inner, container, false);
((TextView) layout.findViewById(R.id.text)).setText(text);
return layout;
}
}
Any help is much appreciated, as the Transition API drives me nuts....
You may show transitions with the setEnterTransition method.
Fragment fragment = InnerTestFragment.getInstance(innerText);
Fade enterFade = new Fade(); //can be any transition, SlideTransition or Inflated Transitions
enterFade.setDuration(300); //Duration of transition in ms
fragment.setEnterTransition(enterFade);
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, fragment)
.addToBackStack(fragment.getClass().getSimpleName())
.commit();
Try to add a background color to the child fragment...

How to get fragment index from within itself?

I have 3 fragments that are generated from the same class file. These three fragments exist within a ViewPager and a FragmentStatePagerAdapter.
They are in sliding tabs. They are created once and never dismissed or deleted or go out of memory.
The app works fine so far, but I need to figure out how to make the fragments have knowledge of their own index (1,2, or 3).
Is there some code like:
int myIndex = summonTheAllKnowingThing.getWhatFragmentThisIsPlease();
That I can add somewhere in the following code?
public class BlankFragment extends Fragment implements View.OnClickListener {
private boolean isDone = false;
EditText persons_name;
Spinner disc1_spinner;
Spinner disc2_spinner;
Spinner disc3_spinner;
Spinner fee1_spinner;
Spinner fee2_spinner;
EditText custom_disc;
EditText custom_fee;
Button doneButton;
public BlankFragment() {
// Required empty public constructor
}
public static BlankFragment newInstance() {
BlankFragment fragment = new BlankFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_blank, container, false);
doneButton = (Button) v.findViewById(R.id.doneButton);
doneButton.setOnClickListener(this);
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// getView().findViewById(R.id.
persons_name = (EditText) getView().findViewById(R.id.NAME);
disc1_spinner = (Spinner) getView().findViewById(R.id.DISC1);
disc2_spinner = (Spinner) getView().findViewById(R.id.DISC2);
disc3_spinner = (Spinner) getView().findViewById(R.id.DISC3);
custom_disc = (EditText) getView().findViewById(R.id.customDISC);
custom_fee = (EditText) getView().findViewById(R.id.customFEE);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.doneButton:
if MainActivity.is1Done
doneButton.setBackgroundColor(Color.RED);
MainActivity.name1 = persons_name.getText().toString();
break;
}
}
}
Best approach is to pass the index while creating the fragment in pager adapter as in
#Override
public Fragment getItem(int position) {
return BlankFragment.newInstance(position);
}
and BlankFragment would be
public class BlankFragment extends Fragment implements View.OnClickListener {
private int myIndex; //<-- access it for getting its index
//.. some other variables
public BlankFragment() {
// Required empty public constructor
}
public static BlankFragment newInstance(int indexInPager) {
BlankFragment fragment = new BlankFragment();
Bundle bundle = new Bundle();
bundle.putInt("index",indexInPager);
fragment.setArguments(bundle);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myIndex = getArguments().getInt("index");
View v = inflater.inflate(R.layout.fragment_blank, container, false);
doneButton = (Button) v.findViewById(R.id.doneButton);
doneButton.setOnClickListener(this);
return v;
}
//.. some other code
}

android args.getString gives null outside onStart

I'm passing a string between two fragments. It's working but for a reason i can't get the string outside onStart...
public class EventSelectFriendFragment extends Fragment {
final static String DATA_RECEIVE = "data_receive";
String passedPushId;
Button create;
#Override
public void onStart() {
super.onStart();
Bundle args = getArguments();
if (args != null) {
passedPushId = args.getString(DATA_RECEIVE);
Log.d("passed id",args.getString(DATA_RECEIVE) );
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_event_select_friend, container, false);
create= (Button) rootView.findViewById(R.id.btnCreate);
btnMaakEvent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int i = selectedFriends.size()-1 ; i >= 0; i--){
Log.d("array: ", i + selectedFriends.get(i).getFirstName());
Log.d("passedPushId: ", passedPushId);
}
}
});
return rootView;
}
}
In onStart passedPushId gives me the string from the other fragment.
But when i want to use it in a for loop the passedPushId is null...
onCreateView() is called earlier than onStart(). Generally speaking, you should initialize instance variables from the arguments Bundle inside of onCreate(), not onStart(), as that is much earlier in the fragment lifecycle.

To get EditText value of another layout xml file [duplicate]

I am trying to get some text from editTexts on different fragments. So what I do first is define my mPager and mPagerAdapter:
a_Atenuacion Activity
public class a_Atenuacion extends FragmentActivity{
private static final int NUM_PAGES = 3;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_dat_viewpager);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new a_Dat_Inicio1();
case 1:
return new a_Dat_Inicio2();
case 2:
return new a_Dat_Inicio3();
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
Then I get my 3 fragments classes (Both 1st and 2nd layouts have an editText, but the 3rd one has an editText and a button). The button function is that when I am in the last fragment (fragment3) it take info form (different editTexts) and send to another activity.
a_Dat_Inicio1 Fragment
public class a_Dat_Inicio1 extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.a_dat_inicio1, container, false);
return view;
}
}
a_Dat_Inicio3 Fragment
public class a_Dat_Inicio3 extends Fragment {
EditText edit3;
EditText edit2;
EditText edit1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.a_dat_inicio3, container, false);
edit1 = (EditText)getActivity().findViewById(R.id.editText1);
final String edit11 = edit1.getText().toString();
edit2 = (EditText)getActivity().findViewById(R.id.editText2);
final String edit22 = edit2.getText().toString();
edit3 = (EditText)view.findViewById(R.id.editText3);
final String edit33 = edit3.getText().toString();
Button but=(Button) view.findViewById(R.id.button);
but.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//Creamos el bundle
Bundle bundle = new Bundle();
//Le ponemos la variable parametro con el contenido (key, valor)
bundle.putString("edit3", edit33);
bundle.putString("edit2", edit22);
bundle.putString("edit1", edit11);
Intent net= new Intent(v.getContext(),Prueba1.class);
net.putExtras(bundle);
startActivity(net);
}
});
return view;
}
}
Finally I get bundle on another activity (Prueba1.class) and it is curious that I only get result for editText1 (on 1st fragment) and rest are null.
Can anybody give me a help?
Thanks in advance.
finally I get over an interface, that is the only way I think to get soemthing on already defined fragments.
My code get like this:
1st define an interface
public interface OnEditTextChanged {
public void onEditPressed1(String edit1);
public void onEditPressed2(String edit1);
public void onEditPressed3(String edit1);
Then fragment activity:
public class a_Dat_Inicio1 extends Fragment {
EditText edit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.a_1dat_inicio1, container, false);
init (view);
return view;
}
OnEditTextChanged editListener;
#Override
public void onAttach(Activity activity){
super.onAttach(activity);
try{
editListener=(OnEditTextChanged) getActivity();
}catch(ClassCastException e){
throw new ClassCastException(activity.toString()+"must implemnt onEditPressed");
}
}
private void init(View view) {
edit=(EditText) view.findViewById(R.id.editText1);
//cada vez que se modifique texto llamar
edit.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
final String edit11 = edit.getText().toString();
editListener.onEditPressed1(edit11);
}
});
And finally on our main activity, call the method:
public class a_Atenuacion extends FragmentActivity implements OnEditTextChanged {
String dat1;
String dat2;
String dat3;
private static final int NUM_PAGES = 3;
private PagerAdapter mPagerAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_1dat_viewpager);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
//HERE DO WHATEVER YOU WANT WITH THE DATA CAUGTH ON THE EDIT 1 METHOD
}
#Override
public void onEditPressed1(String edit1) {
if(mPager.getCurrentItem()==0){
dat1=edit1;
Toast bread = Toast.makeText(getApplicationContext(), "edit1", Toast.LENGTH_LONG);
bread.show();
}
}
HOPE this help someone!!!
In any way thanks!!!
Try this:
In your second fragment, pass the intent extras and your value. For example:
In you second fragment, do this :
Intent someintent = new Intent();
//Some first result
someintent.putExtra("your_value_one", your_value_one);
//Some Second result
someintent.putExtra("your_value_two", your_value_two);
//Some third result
someintent.putExtra("your_value_three", your_value_three);
getActivity().setResult(getActivity().RESULT_OK,someintent);
getActivity().finish();
In your other fragment, where you want this result, do this on your other fragment like this:
1) Make some method to get the info.
private void getInfo() {
//Note: The values are coming from a diff activity or fragment and so the strings should match.
Intent data = new Intent();
String first_value = data.getStringExtra("your_value_one");
String second_value = data.getStringExtra("your_value_two");
String third_value = data.getStringExtra("your_value_three");
Log.i("First Value: ", first_value);
Log.i("First Value: ", second_value);
Log.i("First Value: ", third_value);
}
After making this method, just call it on your onActivityCreated().
Now you can use those string however and wherever you want to. If you want to use those values anywhere else, make sure to define you strings at the very start so that you can use the values anywhere.
Hope this answer helps .. :)

Get EditText value on different fragment

I am trying to get some text from editTexts on different fragments. So what I do first is define my mPager and mPagerAdapter:
a_Atenuacion Activity
public class a_Atenuacion extends FragmentActivity{
private static final int NUM_PAGES = 3;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_dat_viewpager);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new a_Dat_Inicio1();
case 1:
return new a_Dat_Inicio2();
case 2:
return new a_Dat_Inicio3();
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
Then I get my 3 fragments classes (Both 1st and 2nd layouts have an editText, but the 3rd one has an editText and a button). The button function is that when I am in the last fragment (fragment3) it take info form (different editTexts) and send to another activity.
a_Dat_Inicio1 Fragment
public class a_Dat_Inicio1 extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.a_dat_inicio1, container, false);
return view;
}
}
a_Dat_Inicio3 Fragment
public class a_Dat_Inicio3 extends Fragment {
EditText edit3;
EditText edit2;
EditText edit1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.a_dat_inicio3, container, false);
edit1 = (EditText)getActivity().findViewById(R.id.editText1);
final String edit11 = edit1.getText().toString();
edit2 = (EditText)getActivity().findViewById(R.id.editText2);
final String edit22 = edit2.getText().toString();
edit3 = (EditText)view.findViewById(R.id.editText3);
final String edit33 = edit3.getText().toString();
Button but=(Button) view.findViewById(R.id.button);
but.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//Creamos el bundle
Bundle bundle = new Bundle();
//Le ponemos la variable parametro con el contenido (key, valor)
bundle.putString("edit3", edit33);
bundle.putString("edit2", edit22);
bundle.putString("edit1", edit11);
Intent net= new Intent(v.getContext(),Prueba1.class);
net.putExtras(bundle);
startActivity(net);
}
});
return view;
}
}
Finally I get bundle on another activity (Prueba1.class) and it is curious that I only get result for editText1 (on 1st fragment) and rest are null.
Can anybody give me a help?
Thanks in advance.
finally I get over an interface, that is the only way I think to get soemthing on already defined fragments.
My code get like this:
1st define an interface
public interface OnEditTextChanged {
public void onEditPressed1(String edit1);
public void onEditPressed2(String edit1);
public void onEditPressed3(String edit1);
Then fragment activity:
public class a_Dat_Inicio1 extends Fragment {
EditText edit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.a_1dat_inicio1, container, false);
init (view);
return view;
}
OnEditTextChanged editListener;
#Override
public void onAttach(Activity activity){
super.onAttach(activity);
try{
editListener=(OnEditTextChanged) getActivity();
}catch(ClassCastException e){
throw new ClassCastException(activity.toString()+"must implemnt onEditPressed");
}
}
private void init(View view) {
edit=(EditText) view.findViewById(R.id.editText1);
//cada vez que se modifique texto llamar
edit.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
final String edit11 = edit.getText().toString();
editListener.onEditPressed1(edit11);
}
});
And finally on our main activity, call the method:
public class a_Atenuacion extends FragmentActivity implements OnEditTextChanged {
String dat1;
String dat2;
String dat3;
private static final int NUM_PAGES = 3;
private PagerAdapter mPagerAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_1dat_viewpager);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
//HERE DO WHATEVER YOU WANT WITH THE DATA CAUGTH ON THE EDIT 1 METHOD
}
#Override
public void onEditPressed1(String edit1) {
if(mPager.getCurrentItem()==0){
dat1=edit1;
Toast bread = Toast.makeText(getApplicationContext(), "edit1", Toast.LENGTH_LONG);
bread.show();
}
}
HOPE this help someone!!!
In any way thanks!!!
Try this:
In your second fragment, pass the intent extras and your value. For example:
In you second fragment, do this :
Intent someintent = new Intent();
//Some first result
someintent.putExtra("your_value_one", your_value_one);
//Some Second result
someintent.putExtra("your_value_two", your_value_two);
//Some third result
someintent.putExtra("your_value_three", your_value_three);
getActivity().setResult(getActivity().RESULT_OK,someintent);
getActivity().finish();
In your other fragment, where you want this result, do this on your other fragment like this:
1) Make some method to get the info.
private void getInfo() {
//Note: The values are coming from a diff activity or fragment and so the strings should match.
Intent data = new Intent();
String first_value = data.getStringExtra("your_value_one");
String second_value = data.getStringExtra("your_value_two");
String third_value = data.getStringExtra("your_value_three");
Log.i("First Value: ", first_value);
Log.i("First Value: ", second_value);
Log.i("First Value: ", third_value);
}
After making this method, just call it on your onActivityCreated().
Now you can use those string however and wherever you want to. If you want to use those values anywhere else, make sure to define you strings at the very start so that you can use the values anywhere.
Hope this answer helps .. :)

Categories

Resources