How to update TabLayout on return from activity

My app I am working on has 3 tabs in the TabLayout. Each of them consists of a list. Also, I am using FragmentStatePageAdapter and a custom adapter. When you click on an item in the ListView, it triggers the action. After you have completed the task in your work, it will update some data in the array. The user can then press the Back button on their phone to return. This is where the problem lies.

Now Tab1 never changes, later I want to implement this return which sets it to Tab1. This is a later alarm. The problem is the middle tab doesn't reload. I understand the reason is because the middle tab is already created so visually, the swipe effect looks accurate. Tab3 will update when navigating from Tab1 because it hasn't loaded yet.

I've tried different listeners working with onResume in the tablayout fragment. I have no notifydatasetch anywhere in my code and have failed to implement this. I suppose that I somehow implemented something wrong and overcame the correct solution because I tried so many things.

Here is some code to help me figure out how I have everything set up.


MainActivity

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(0);

    //more

    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    tabLayout.setScrollPosition(0, 0f, true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@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.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

      

SectionsPagerAdapter

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
protected Context mContext;

public SectionsPagerAdapter( FragmentManager fm) {
    super(fm);

}

@Override
public Fragment getItem(int position) {
    // getItem is called to instantiate the fragment for the given page.
    // Return a PlaceholderFragment (defined as a static inner class below).
    int number = position;
    switch (number) {
        case 0: // Fragment # 0 - This will show FirstFragment
            return TabFragment1.newInstance();
        case 1: // Fragment # 0 - This will show FirstFragment different title
            return TabFragment2.newInstance();
        case 2: // Fragment # 1 - This will show SecondFragment
            return TabFragment3.newInstance();
        default:
            return null;
    }

    // return PlaceholderFragment.newInstance(position + 1);
}

@Override
public int getCount() {
    // Show 3 total pages.
    return 3;
}


@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "All";
        case 1:
            return "Unfinished";
        case 2:
            return "Completed";
    }
    return null;
}}

      

MyAdapter

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
protected Context mContext;

public SectionsPagerAdapter( FragmentManager fm) {
    super(fm);

}

@Override
public Fragment getItem(int position) {
    // getItem is called to instantiate the fragment for the given page.
    // Return a PlaceholderFragment (defined as a static inner class below).
    int number = position;
    switch (number) {
        case 0: // Fragment # 0 - This will show FirstFragment
            return TabFragment1.newInstance();
        case 1: // Fragment # 0 - This will show FirstFragment different title
            return TabFragment2.newInstance();
        case 2: // Fragment # 1 - This will show SecondFragment
            return TabFragment3.newInstance();
        default:
            return null;
    }

    // return PlaceholderFragment.newInstance(position + 1);
}

@Override
public int getCount() {
    // Show 3 total pages.
    return 3;
}


@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "All";
        case 1:
            return "Unfinished";
        case 2:
            return "Completed";
    }
    return null;
}}

      

Fragment1 (all three fragments are exact)

public class TabFragment1 extends android.support.v4.app.Fragment {

public ArrayList<String> mylist1 = new ArrayList<>();

// newInstance constructor for creating fragment with arguments
public static TabFragment1 newInstance() {
    TabFragment1 fragmentFirst = new TabFragment1();
    Bundle args = new Bundle();
    //pass a list or something?
    fragmentFirst.setArguments(args);
    return fragmentFirst;
}

// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    mylist1.clear();
    for (int i = 0; i < arrayInt.length; i++) {

            mylist1.add(array[i]);

    }


    final ListView listView = (ListView) rootView.findViewById(R.id.text_label);

    MyAdapter adapter = new MyAdapter(getContext(), mylist1);

    listView.setAdapter(adapter);

    //Click on Item
    //selecting items
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //goes to new activity passing the item name
            Intent intent = new Intent(view.getContext(), MapsActivity.class);
            Bundle b = new Bundle();

            //get text for current item
            //String textGet = listView.getItemAtPosition(position).toString();
            String textGet = mylist1.get(position);
            //put text into a bundle and add to intent
            intent.putExtra("text", textGet);

            //get position to carry integer
            intent.putExtra("position", position);

            intent.putExtras(b);

            //begin other activity
            startActivity(intent);
        }
    });

    return rootView;
}

      

Here is an older attempt at changing the dataset, but no luck. I put this in the snippet itself.

 public void onResume(){
    super.onResume();

  Log.e("Frag2", "Refresh Test");

    // a check so it doesnt run first time starting app and crashing
    if (g != 0) {
        updateData();
    }
    g = 1;

}

private void updateData(){
    Log.e("Frag2", "Refresh Test");
    this.mylist2.clear();
    for (int i = 0; i < arrayInt.length; i++) {
        if (arrayInt[i] == 1) {
            this.mylist2.add(array[i]);
        }
    }

    adapter = new MyAdapter(getContext(), this.mylist2);
    adapter.notifyDataSetChanged();

}

      

+3


source to share





All Articles