Pass Data From One Design Tab To Another Tab
Solution 1:
Each fragment is associated with the parent activity. so you can't directly communicate from one fragment to another fragment. You will need to go through Parent Activity using interface.
Check this docs : https://developer.android.com/training/basics/fragments/communicating.html
On button click pass the value to methods in your custom interface and access those methods in second fragment.
when I try to swipe to FragmentTwo nothing gets called in FragmentTwo
For this you need to implement fragment life cycle - https://developer.android.com/reference/android/app/Fragment.html
UPDATE
Done with some modification in you code, just look at the foll. code -
Manifex.xml
<applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".MainActivity"android:theme="@style/Theme.AppCompat.NoActionBar"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
MainActivity.java
package com.app.onkar.tabdemo;
import android.support.v4.app.Fragment;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
publicclassMainActivityextendsAppCompatActivityimplementsViewPager.OnPageChangeListener {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
ViewPagerAdapteradapter=newViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(newFragmentOne(), "ONE");
adapter.addFragment(newFragmentTwo(), "TWO");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(this);
tabLayout.setupWithViewPager(viewPager);
}
@OverridepublicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@OverridepublicvoidonPageSelected(int position) {
System.out.println("onPageSelected Called");
}
@OverridepublicvoidonPageScrollStateChanged(int state) {
}
publicstaticclassViewPagerAdapterextendsFragmentPagerAdapter {
privatefinal List<Fragment> mFragmentList = newArrayList<>();
privatefinal List<String> mFragmentTitleList = newArrayList<>();
privatestaticintcount=2;
publicvoidaddFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
publicViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Overridepublic Fragment getItem(int position) {
switch (position) {
case0:
returnnewFragmentOne();
case1:
returnnewFragmentTwo();
}
returnnull;
}
@OverridepublicintgetCount() {
return count;
}
@Overridepublic CharSequence getPageTitle(int position) {
switch (position) {
case0:
return"Tab One";
case1:
return"Tab Two";
}
returnnull;
}
}
}
FragmentOne.java
package com.app.onkar.tabdemo;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
publicclassFragmentOneextendsFragment {
private EditText editText;
private Button btnSendData;
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("IN FRAGMENT ONE");
Viewview= inflater.inflate(R.layout.fragment_one,container,false);
editText = (EditText) view.findViewById(R.id.et_name);
btnSendData = (Button) view.findViewById(R.id.btn_send);
btnSendData.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
FragmentTwofragment=newFragmentTwo();
Bundlebundle=newBundle();
bundle.putString("username",editText.getText().toString());
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.second_frag,fragment).commit();
}
});
return view;
}
}
FragmentTwo.java
package com.app.onkar.tabdemo;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
publicclassFragmentTwoextendsFragment {
@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("IN FRAGMENT TWO");
Viewview= inflater.inflate(R.layout.fragment_two,container,false);
TextViewtxt2= (TextView) view.findViewById(R.id.textView2);
Bundlebundle= getArguments();
if(bundle!= null)
{
Stringvalue= getArguments().getString("username");
txt2.setText(value);
}
return view;
}
@OverridepublicvoidonResume() {
super.onResume();
System.out.println("onResume gets called");
}
}
No change in Layout files. Just try above code - it is working exactly as you want. Hope it will help!
Solution 2:
ViewPagerAdapter class
publicclassViewPagerAdapterextendsFragmentPagerAdapter {
privatestaticintcount=2;
publicViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Overridepublic Fragment getItem(int position) {
switch (position) {
case0:
Fragmentfragmentone=newFragmentOne();
Bundleargs=newBundle();
args.putInt("code", 1);
fragmentone.setArguments(args);
return fragmentone;
break;
case1:
Fragmentfragmenttwo=newFragmentTwo();
Bundleargs=newBundle();
args.putInt("code", 2);
fragmenttwo.setArguments(args);
return fragmenttwo ;
break;
}
returnnull;
}
@OverridepublicintgetCount() {
return count;
}
@Overridepublic CharSequence getPageTitle(int position) {
switch (position)
{
case0 :
return"Tab One";
case1 :
return"Tab Two";
}
returnnull;
}
}
args is the bundle object, you can put String int and other values to use these values in fragment use below code in onCreateView method of Fragment.
int codeForthisFragment = getArguments().getInt("code");
for button click: FragmentTransaction ft = getFragmentManager().beginTransaction(); Fragment secondFragment = new FragmentTwo();
Bundleargs=newBundle();
args.putInt("code", 2);
secondFragment.setArguments(args);
ft.replace(R.id.content_frame, secondFragment);
ft.commit();
Solution 3:
In the second fragment i am getting data buts its adding another view with previous one. see the image
With reference to the answer by @Roy Miller(https://stackoverflow.com/users/5255021/roy-miller), you can solve it by doing the follwing changes:
In MainActivity.java ,replace the follwing switch:
@Overridepublic Fragment getItem(int position) {
switch (position) {
case0:
returnnewFragmentOne();
case1:
returnnewFragmentTwo();
}
returnnull;
}
With:
@Override
public Fragment getItem(int position) {
switch (position){
case0:
return mFragmentList.get(0);
case1:
return mFragmentList.get(1);
case2:
return mFragmentList.get(2);
default:
returnnull;
}
}
And then, in FragmentOne.java
Replace this:
btnSendData.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
FragmentTwofragment=newFragmentTwo();
Bundlebundle=newBundle();
bundle.putString("username",editText.getText().toString());
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.second_frag,fragment).commit();
}
});
With:
btnSendData.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
/*This one FragmentCollection is a Singleton class that contains list of fragments, set it during initialization itself(code is attached below this code)*/
FragmentCollection fragmentCollection=FragmentCollection.getInstance();
FragmentTwofragment= (FragmentTwo) FragmentCollection.getmFragmentList().get(2); //getting the second fragment
fragment.setText(editText.getText().toString()); //add this method in fragment two
}
});
Now inside FragmentTwo.java Add the method:
publicvoidupdateTextView(String data){
Log.d("TabTwo","Update Text view");
TextView txt = (TextView)view.findViewById(R.id.textView2);
txt.setText(data);
}
At last, the singleton class i mentioned above:
publicclassFragmentCollection {
static FragmentCollection fragmentCollection=new FragmentCollection();
private final List<Fragment> mFragmentList = new ArrayList<>();
privateFragmentCollection(){}
publicstatic FragmentCollection getInstance(){
if(fragmentCollection!=null) {
return fragmentCollection;
}else {
returnnew FragmentCollection();
}
}
publicvoidsetmFragmentList(List<Fragment> mFragmentList){
this.mFragmentList.addAll(mFragmentList);
}
public List<Fragment> getmFragmentList(){
return mFragmentList;
}
}
The issue of getting older views overlapping is because of initializing the fragment again and again
like new FragmentTwo(); in more than one place.
So here we put it in a list and access it so only a single fragment is altered.
Solution 4:
I am not able to go to first fragment from Second fragment . I am using tab layout This is my code By clicking button from 2nd Fragment
Bundle bundle = new Bundle();
bundle.putString("uniqueList1", uniqueList.get(0));
bundle.putString("uniqueList2", uniqueList.get(1));
bundle.putString("kycUniquesNo", kycUniqueNum);
CustBasicFormFragement fragObj = new CustBasicFormFragement();
fragObj.setArguments(bundle);
In 1st Fragment
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
binding = DataBindingUtil.inflate(inflater, R.layout.cust_basic_form_fragment, container, false);
view = binding.getRoot();
if (getArguments() != null) {
string1 = getArguments().getString("uniqueList1");
string2 = getArguments().getString("uniqueList2");
kycUnique=getArguments().getString("kycUniquesNo");
System.out.print(string1);
System.out.print(string2);
System.out.print(kycUnique);
return view;
}
Post a Comment for "Pass Data From One Design Tab To Another Tab"