mstafkmx
mstafkmx

Reputation: 429

java.lang.IllegalStateException activity with viewPager and tabs

When I go back from the next activity, always savedInstanceState is null and when I rotate screen or turn back , I get this error: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. I have tried to use in MyFragment View fragment = (View) inflater.inflate(R.layout.recycler_view, null); Or View fragment = (View) inflater.inflate(R.layout.recycler_view, container, false); or View fragment = (View) inflater.inflate(R.layout.recycler_view, container); but i get the same error.

Error log:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                                at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
                                                                                at android.view.ViewGroup.addView(ViewGroup.java:3415)
                                                                                at android.support.v4.view.ViewPager.addView(ViewPager.java:1342)
                                                                                at android.view.ViewGroup.addView(ViewGroup.java:3360)
                                                                                at android.view.ViewGroup.addView(ViewGroup.java:3336)
                                                                                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1094)
                                                                                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
                                                                                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
                                                                                at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                                at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                                at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:447)
                                                                                ....

MainActivity

        public class MainActivity extends AppCompatActivity {
            static public DrawerLayout mDrawerLayout;
            private ArrayList<ArrayList<Article>> articlesHomeList;

@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Log.d("--MainActivity", "onSaveInstanceState");
        // Save the user's current game state
        savedInstanceState.putSerializable("articlesHomeList", articlesHomeList);
        savedInstanceState.putString("test", "teststetststs");
        // Always call the superclass so it can save the view hierarchy state
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
        Log.d("--MainActivity", "onRestoreInstanceState");
        super.onRestoreInstanceState(savedInstanceState, persistentState);
        this.articlesHomeList = (ArrayList<ArrayList<Article>>) savedInstanceState.getSerializable("articlesHomeList");
    }

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_main);
                Bundle bundle = getIntent().getExtras();
                articlesHomeList = (ArrayList<ArrayList<Article>>) bundle.getSerializable("articlesHomeList");
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                toolbar.setTitle(R.string.app_name);
                setSupportActionBar(toolbar);


                // Setting ViewPager for each Tabs
                ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
                setupViewPager(viewPager);
                // Set Tabs inside Toolbar
                TabLayout tabs = (TabLayout) findViewById(R.id.tabs);

                tabs.setupWithViewPager(viewPager);


                    }

            private void setupViewPager(ViewPager viewPager) {

                Bundle bundleAll = new Bundle();
                bundleAll.putSerializable("articles", (Serializable) this.articlesHomeList.get(2));
                myFragment MyFragment = new MyFragment();
                myFragment.setArguments(bundleAll);

                AdapterTabs adapter = new AdapterTabs(getSupportFragmentManager());
                adapter.addFragment(myFragment, "All");

                viewPager.setAdapter(adapter);
                viewPager.setCurrentItem(0);
            }
    @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();
            Log.e("Item selected", Integer.toString(id));
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            } else if (id == android.R.id.home) {
                mDrawerLayout.openDrawer(GravityCompat.START);
            }
            return super.onOptionsItemSelected(item);
        }

myFragment:

    public class MyFragment extends Fragment {
        ...

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View fragment = (View) inflater.inflate(R.layout.recycler_view, container, false);
            recyclerView = (RecyclerView) fragment.findViewById(R.id.my_recycler_view);
            articlesList = (List<Article>) this.getArguments().getSerializable("articles");

            ContentAdapter adapter = new ContentAdapter(articlesList);
            recyclerView.setAdapter(adapter);
            recyclerView.setHasFixedSize(true);

            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            return recyclerView;
        }
...
}

EDIT

I have added to MainActivity:

if (savedInstanceState != null) {
            this.articlesHomeList = (ArrayList<ArrayList<Article>>) savedInstanceState.getSerializable("articlesHomeList");
            this.renderTabs(viewPager);
        } else if (getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            articlesHomeList = (ArrayList<ArrayList<Article>>) bundle.getSerializable("articlesHomeList");
            this.renderTabs(viewPager);
        } else {
            this.loadArticlesHome(viewPager);
        }

But always savedInstanceState is null.

Upvotes: 1

Views: 678

Answers (1)

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17142

In the onCreateView method of your MyFragment fragment, change

return recyclerView;

to

return fragment;

You should return the fragment's view container, not the RecyclerView's. You see : the onCreateView method is called to have the fragment instantiate its user interface view, not just a single component of it (in your case the RecyclerView).

EDIT:

As @BlackBelt said, the app crashes because the RecyclerView has already a parent, defined in the layout, and it can't have two (the one in the layout and the ViewPager) : which is clearly what the exception says

java.lang.IllegalStateException: The specified child already has a parent

Upvotes: 2

Related Questions