2

我有一个包含多个选项卡的选项卡活动x。所有选项卡都在第一次加载时正确加载。如果我将x+2选项卡从选项卡上移开然后返回,则会丢失一些数据和元素。

我使用了 Android Studio 自己Tabbed Activity生成的模板,以及标签的 Fragment 模板。

我已经审查了SO和其他一些,但它们似乎并不完全适合我的模型。或者如果他们这样做,我没有看到它。

有什么帮助吗?

这是我的主选项卡式活动,其中包含一些相关的导入。

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

public class MasterTabActivity extends AppCompatActivity {

    public static Intent newIntent(Context packageContext) {
        Intent intent = new Intent(packageContext, MasterTabActivity.class);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        return intent;
    }


    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master_tab);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            //get some data
        }


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar ab = getSupportActionBar();
        if (ab != null) {
            ab.setDisplayHomeAsUpEnabled(true);
        }

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        // Titles
        tabLayout.addTab(tabLayout.newTab().setText("Details"));
        tabLayout.addTab(tabLayout.newTab().setText("Photos"));
        tabLayout.addTab(tabLayout.newTab().setText("Notes"));

        //Add ICONS TO TEXT
        tabLayout.getTabAt(0).setIcon(R.drawable.ic_details);
        tabLayout.getTabAt(1).setIcon(R.drawable.ic_photos);
        tabLayout.getTabAt(2).setIcon(R.drawable.ic_notes);

        tabLayout.setTabTextColors(
                ContextCompat.getColor(this, R.color.colorPrimary), //unselected
                ContextCompat.getColor(this, R.color.colorDarkText) //selected
                                  );

        //Set the initial selected icons tab color
    tabLayout.getTabAt(0).getIcon().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorDarkText), PorterDuff.Mode.SRC_IN);
        tabLayout.getTabAt(1).getIcon().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
        tabLayout.getTabAt(2).getIcon().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);



        // tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
                tab.getIcon().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorDarkText), PorterDuff.Mode.SRC_IN);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

                tab.getIcon().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

                }
            });

refreshData();

        }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return DetailsFragment.newInstance(createdCallKey);
                case 1:
                    return PhotosFragment.newInstance(createdCallKey);
                case 2:
                    return NotesFragment.newInstance(createdCallKey);
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 3;
        }
    }
}

以及碎片样本。在这一点上,它们的设置几乎相同,只是保存了一些基本的 GUI 元素。

import android.support.v4.app.Fragment;

public class DetailsFragment extends Fragment {

    public DetailsFragment() {
        // Required empty public constructor
    }

    public static DetailsFragment newInstance(String callKey) {
        DetailsFragment fragment = new DetailsFragment();
        Bundle          args     = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_details, container, false);
    //do all the view setup 
        return rootView;

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}
4

1 回答 1

0

在我的Fragments我查询Tabbed Activity一些数据。当片段移开多个x+2标签时,将onPause调用 Fragments 方法(以及其他拆卸方法)。检查片段的所有阶段的生命周期文档

当您返回选项卡时,该onCreateView方法(以及其他构建方法)显示 1onAttach1 和onCreate。在我上面的示例中,片段查询 .Activity 中的 Activity onCreate

所以我创建了一个 refreshData 方法:

 private void refreshData() {
                 someTextElement.setText(((MasterTabActivity) getActivity()).getCallDetailsDictionary().getString("CALdCallTaken").getString("SomeKey"));
    } 

并将调用移至refreshDataonStart首先检查活动中的数据不为空。

  @Override
    public void onStart() {
        super.onStart();
        Log.w(TAG, "-------------- onStart");
        if (((MasterTabActivity) getActivity()).getCallDetailsDictionary() != null) {
            refreshData();
        }

    }

现在,只要该视图返回视图,它就会调用onStart并刷新其数据。

于 2018-03-26T14:52:11.573 回答