android java片段生命周期

snvhrwxg  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(358)

我使用一个bundle在片段之间传输数据,数据传输成功。但是,如果屏幕从fragment1更改为fragment2,fragment1的生命周期将变为ondestroy,写入edittext的内容不应该被删除吗?我想在片段切换时删除edittext的内容。从gif中可以看到,在这里输入图像描述,即使片段1的生命周期是ondestroy,当它返回到片段1时,值仍保留在edittext中。
主活动.java

package com.example.fragmentbundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button frag1,frag2;
    Fragment1 fragment1;
    Fragment2 fragment2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frag1 = (Button)findViewById(R.id.frag1);
        frag2 = (Button)findViewById(R.id.frag2);
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
    }

    @Override
    protected void onResume() {
        super.onResume();
        frag1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frameLayout,fragment1).commit();
            }
        });
        frag2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frameLayout,fragment2).commit();
            }
        });
    }
}

碎片1.java

package com.example.fragmentbundle;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

public class Fragment1 extends Fragment {
    private Button submit;
    private EditText putfrag1;
    private TextView getfrag1;
    private Fragment2 fragment2;
    MainActivity activity;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (MainActivity)getActivity();

        Toast.makeText(activity,"onCreate()",Toast.LENGTH_SHORT).show();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_layout1,container,false);
        fragment2 = new Fragment2();
        putfrag1 = (EditText)rootView.findViewById(R.id.put_frag1);
        getfrag1 = (TextView) rootView.findViewById(R.id.get_frag1);
        submit = (Button)rootView.findViewById(R.id.submit);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( getFragmentManager() != null) {
                    Bundle bundle = new Bundle();
                    String getText = putfrag1.getText().toString();
                    bundle.putString("frag1",getText);//Bundle data to fragment 2, transfer and commit
                    fragment2.setArguments(bundle);
                    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.frameLayout,fragment2).commit();
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        Toast.makeText(activity,"onPause()",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Toast.makeText(activity,"onDestroyView()",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(activity,"onDestroy()",Toast.LENGTH_SHORT).show();
    }
}

碎片2.java

package com.example.fragmentbundle;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Fragment2 extends Fragment {
    private TextView textView;
    private Button button;
    MainActivity activity;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (MainActivity)getActivity();

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_layout2,container,false);
        textView = (TextView)rootView.findViewById(R.id.getfrag2);
        button = (Button)rootView.findViewById(R.id.submit);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getArguments()!=null){ //When getArgument is not null, get the transmitted data and setText to textView
            String getText = getArguments().getString("frag1");
            Toast.makeText(activity,getText,Toast.LENGTH_SHORT).show();
            textView.setText(getText);
        }
    }
}

在此处输入图像描述

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题