如何确保碎片不被破坏?

时间:2019-12-09 15:48:14

标签: android android-fragments

我有四个碎片,我想确定的是,当我在它们之间滑动时,它们都没有被破坏。

我正在尝试确定生命周期:https://developer.android.com/reference/android/app/Fragment.html#lifecycle

我是否需要在将片段放入onPause后立即放置“ setRetainInstance”?我是从片段还是从持有所有片段或PagerAdapter的活动中执行此操作?

这是我的mainActivity。

package com.example.login3.YouPage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.login3.R;
import com.google.android.material.tabs.TabLayout;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class MainActivity extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;
    private FirebaseDatabase firebaseDatabase;
    private FirebaseStorage firebaseStorage;
    private DatabaseReference mUsersDatabase;

    private ImageView profilePic;
    private TextView profileName;
    private Button profileEdit;
    private Button logout;
    private Button updatePassword;
    private Button updateEmail;

    private ViewPager mViewPager;
    private SectionsPagerAdapter mSectionsPagerAdapter;

    private TabLayout mTabLayout;

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

        //tabs
        mViewPager = findViewById(R.id.main_tabPager);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mTabLayout = findViewById(R.id.main_tabs);
        mTabLayout.setupWithViewPager(mViewPager);


        firebaseAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();
        firebaseStorage = FirebaseStorage.getInstance();

    }


}

这是我的PagerAdapter

package com.example.login3.YouPage;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.login3.FeedFragment;
import com.example.login3.FriendsFragment;
import com.example.login3.NotifsFragment;
import com.example.login3.YouFragment;

class SectionsPagerAdapter extends FragmentPagerAdapter {


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


    @Override
    public Fragment getItem(int position) {

        switch (position){
            case 0:
                return new NotifsFragment();
            case 1:
                FeedFragment feedFregment = new FeedFragment();
                return feedFregment;
            case 2:
                FriendsFragment friendsFragment = new FriendsFragment();
                return friendsFragment;
            case 3:
                YouFragment youFragment = new YouFragment();
                return youFragment;

                default:
                    return null;
        }
    }

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

    //Titles on pages.
     public CharSequence getPageTitle(int position){
        switch (position){
            case 0:
                return "Notifs";
            case 1:
                return "Feed";
            case 2:
                return "Friends+";
            case 3:
                return "You";

            default:
                return null;
        }
    };

}

或者还有另一种更正确的方法来阻止碎片被破坏吗? 最好的问候

1 个答案:

答案 0 :(得分:0)

您可以直接在Viewpager上使用mViewPager.setOffscreenPageLimit(4);

setOffscreenPageLimit 在版本22.1.0中添加

void setOffscreenPageLimit(整数限制) 设置在空闲状态下视图层次结构中应保留到当前页面任一侧的页面数。超出此限制的页面将在需要时从适配器重新创建。

来自https://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)