旧的片段数据因帧布局中的活动率而膨胀

时间:2018-12-28 12:18:47

标签: android android-fragments

我有两个活动Activity A和ActivityB。活动A中有两个按钮,创建和编辑,活动B中有三个片段,带有edittext和textviews和一个下一个按钮。

活动A功能: 当用户单击“编辑”按钮时,它会故意发送一个“ id”;当用户单击“创建”按钮时,它将发送“ -1”的ID。

活动B功能: 片段事务通过getSupportFragmentManager()完成。当收到id> 0时,它将从数据库中获取数据并在所有三个片段中显示它,否则将不会获取任何内容。

在单击“编辑”按钮时,所有先前的数据都将恢复,如果我按“后退”按钮,则将事务提交给片段B后。重新创建活动A并完成活动B。

问题是,如果现在单击创建按钮,则所有片段中的旧数据都将恢复。

活动A:-

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_section_home);
//        if (savedInstanceState != null) {
//            return;
//        }
        AppViewModel appViewModel = ViewModelProviders.of(this).get(AppViewModel.class);

        projId = getIntent().getIntExtra(Utils.PROJECT_ID, -1);
        projName = getIntent().getStringExtra(Utils.PROJECT_NAME);
        projNameFull = getIntent().getStringExtra(Utils.PROJECT_NAME_FULL);

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

        Button btnCreateSec = findViewById(R.id.btnCreateSec);
        RecyclerView rvSectionList = findViewById(R.id.rvSectionList);
        final secListAdapter mAdapter = new secListAdapter();
        rvSectionList.setAdapter(mAdapter);

        if (projId>0) {
            appViewModel.fetchSections(projId).observe(this, new Observer<List<SectionInfoEntity>>() {
                @Override
                public void onChanged(List<SectionInfoEntity> sectionInfoEntities) {
                    sectionInfoEntityList = sectionInfoEntities;
                    mAdapter.setList(sectionInfoEntities);
                }
            });
        }

        btnCreateSec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i  = new Intent(SectionHome.this, SectionCreate.class);
                startActivity(i);
                overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left);
                finish();
            }
        });

        btnEditSec.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i  = new Intent(SectionHome.this, SectionCreate.class);
                i.putExtra(Utils.SECTION_ID,sec_list.getId());
                    startActivity(i);
                    overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left);
                    finish();
                }
            });
    }

活动B

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

    secId = getIntent().getIntExtra(Utils.SECTION_ID,-1);

    if (secId != -1){
        sectionInfoEntity = appViewModel.fetchSectionById(secId);
    }
    else{
        sectionInfoEntity = new SectionInfoEntity();
    }

    Button btnNext = findViewById(R.id.btnNext);
    Button btnBack = findViewById(R.id.btnBack);

    btnNext.setOnClickListener(this);
    btnBack.setOnClickListener(this);

    manager = getSupportFragmentManager();
    Fragment a = manager.findFragmentByTag(fragSecCreateTag);

    manager.beginTransaction().add(R.id.flContainer, fragSecCreate,fragSecCreateTag).commit();
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btnNext:
            if (manager.findFragmentByTag(fragSecCreateTag) instanceof SecCreateFragment) {
                SecCreateFragment f = (SecCreateFragment) manager.findFragmentByTag(fragSecCreateTag);
                if (f != null) {
                    if (f.checkData()) {
                        manager.beginTransaction().
                                setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left).
                                replace(R.id.flContainer, fragSecDetail, fragSecDetailTag).
                                commit();
                    }
                }
            }
            else if (manager.findFragmentByTag(fragSecDetailTag) instanceof SecDetailFragment){
                SecDetailFragment f = (SecDetailFragment) manager.findFragmentByTag(fragSecDetailTag);
                if (f != null) {
                    if (f.checkData()) {
                        manager.beginTransaction().
                                setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left).
                                replace(R.id.flContainer, fragSecDetailSummary,fragSecDetailSummaryTag).
                                commit();
                    }
                }
            }
            else if (manager.findFragmentByTag(fragSecDetailSummaryTag) instanceof SecDetailSummaryFragment){

                int SectionId;
                if (secId != -1)
                {
                    appViewModel.updateSection(sectionInfoEntity);
                    SectionId = secId;
                }
                else {
                    SectionId = (int) appViewModel.addSection(sectionInfoEntity);
                }
                Intent i  = new Intent(SectionCreate.this, SectionSurvey.class);
                i.putExtra(Utils.PROJECT_ID,projId);
                i.putExtra(Utils.PROJECT_NAME_FULL,projNameFull);
                i.putExtra(Utils.SECTION_ID, SectionId);
                i.putExtra(Utils.SECTION_NAME,secName);
                i.putExtra(Utils.SECTION_TYPE,secType);
                i.putExtra(Utils.SECTION_LENGTH,secLen);
                i.putExtra(Utils.SECTION_MILESTONE,secKm);
                startActivity(i);
                overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left);
                finish();
            }
            manager.executePendingTransactions();
            break;
        case R.id.btnBack:
            if (manager.findFragmentByTag(fragSecDetailSummaryTag) instanceof SecDetailSummaryFragment){
                manager.beginTransaction().
                        setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right).
                        replace(R.id.flContainer, fragSecDetail,fragSecDetailTag).
                        commit();
            }
            else if (manager.findFragmentByTag(fragSecDetailTag) instanceof SecDetailFragment){
                manager.beginTransaction().
                        setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right).
                        replace(R.id.flContainer, fragSecCreate,fragSecCreateTag).
                        commit();
            }
            else if (manager.findFragmentByTag(fragSecCreateTag) instanceof SecCreateFragment){
                Intent i  = new Intent(SectionCreate.this, SectionHome.class);
                i.putExtra(Utils.PROJECT_ID, projId);
                i.putExtra(Utils.PROJECT_NAME,projName);
                startActivity(i);
                overridePendingTransition(R.anim.enter_from_left, R.anim.exit_to_right);
                finish();
            }
            manager.executePendingTransactions();
            break;
    }
}

片段1和2

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.frag_sec_create, container, false);

    etSecName = v.findViewById(R.id.etSecName);
    RadioGroup rgSecStruct = v.findViewById(R.id.rgSecStruct);
    RadioButton rb = v.findViewById(R.id.rbDivided);
    RadioButton rb1 = v.findViewById(R.id.rbUndivided);

        if (secId!=-1 && !secName.equals("")){
            etSecName.setText(secName);
            etSecName.setEnabled(false);
            secDate = sec_Date;
            SectionInfoEntity sectionInfoEntityEdit = SectionCreate.sectionInfoEntity;
            SecStruc = sectionInfoEntityEdit.getSstructure();
            SecType = sectionInfoEntityEdit.getStype();
            if (SecStruc.equals(rb1.getText().toString())){
                rb1.setChecked(true);
                for(int i =0; i< SectionTypeUndividedList.size(); i ++){
                    if (SecType.equals(SectionTypeUndividedList.get(i).getType())){
                        row_index = i;
                        break;
                    }
                }
            }
            else {
                rb.setChecked(true);
                for(int i =0; i< SectionTypeDividedList.size(); i ++){
                    if (SecType.equals(SectionTypeDividedList.get(i).getType())){
                        row_index = i;
                        break;
                    }
                }
            }
            rb.setEnabled(false);
            rb1.setEnabled(false);
        }
        else {
            rb.setChecked(true);
            SecStruc = rb.getText().toString();
        }

    return v;
}

0 个答案:

没有答案