import android.widget.Button;
public class MainFragment extends Fragment {
public MainFragment() {
// Bo'sh constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
// Buttonni boshlash
Button goToFragmentButton = view.findViewById(R.id.goToFragmentButton);
goToFragmentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Yangi fragmentni yaratib ko'rsatish
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new SecondFragment())
.addToBackStack(null) // Fragmentlar orasida o'tkazishni saqlash
.commit();
}
});
return view;
}
}
SecondFragment.java:
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class SecondFragment extends Fragment {
public SecondFragment() {
// Bo'sh constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_second, container, false);
Do'stlaringiz bilan baham: |