I heve read all the answers related this.
I have 2 buttons, both opens different fragment.
when I open a fragment, do something with it, open another fragment and come back to the first fragment then I see all the changes I made are not applied, because fragment restarted. Please help me..
Activity
package com.example.canvastag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.canvastag.fragments.firstFrag;
import com.example.canvastag.fragments.secondFrag;
public class asking extends AppCompatActivity {
firstFrag frag1;
secondFrag frag2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_asking);
Button button = findViewById(R.id.frag1button);
Button button2 = findViewById(R.id.frag2button);
frag1 = new firstFrag();
frag2 = new secondFrag();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, frag1, "").commit();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, frag2, "").commit();
}
});
}
}
Fragment 1
public class firstFrag extends Fragment {
View view;
TextView textView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_first, container, false);
textView=view.findViewById(R.id.textFrag);
Button button = view.findViewById(R.id.buttonAdd);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("button clicked");
}
});
return view;
}
}
Fragment 2-
public class secondFrag extends Fragment {
View view;
TextView textView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_second, container, false);
textView=view.findViewById(R.id.textFrag);
Button button = view.findViewById(R.id.buttonAdd);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("button seocnd clicked");
}
});
return view;
}
}
I want to resume fragment when I come back to it, not restart.
OUTPUT-
Thank you so much...
Instead of:
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, frag1, "").commit();
Try using:
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, frag1, "").commit();
Note:
You can add this to the fragment transaction: addToBackStack(null)
so your previous state will be added to the backstack allowing you to go back with the back button.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.