I have created a android app in which I pass data from MainAcitivity to NavAcitivity using intents. However, I further pass the data to a fragment named HomeFragment using bundle(). I check in the HomeFragment if I got the arguments and depending on that display the data.
HomeFragment.java
public class HomeFragment extends Fragment {
private FragmentHomeBinding binding;
database d = new database();
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
if (getArguments() != null) {
String userKey = getArguments().getString("userKey");
String passKey = getArguments().getString("passKey");
Log.i("Data", "onCreateView: Data received: " + userKey + " " + passKey);
View root = binding.getRoot();
TextView textViewDetails = root.findViewById(R.id.textViewDetails);
textViewDetails.setText(d.display(userKey,passKey));
}
else Log.i("Data", "onCreateView: Data Not received");
return binding.getRoot();
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
NavAcitivity.java
public class NavActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
com.example.combinedapp.databinding.ActivityNavBinding binding = ActivityNavBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_nav);
NavigationUI.setupWithNavController(binding.navView, navController);
Intent intent = getIntent();
String userKey = intent.getStringExtra(MainActivity.userKey);
String passKey = intent.getStringExtra(MainActivity.passKey);
Bundle bundle = new Bundle();
bundle.putString("userKey", userKey);
bundle.putString("passKey", passKey);
navController.setGraph(R.navigation.mobile_navigation, bundle);
navController.navigate(R.id.navigation_home, bundle);
}
}
However when I run the app and on the HomeFragment I click the back button then it shows another HomeFragment with no data displayed of the user and again on back button click, it goes to the login page. The ideal behaviour should be that it goes directly from HomeFragment to Login page in one single click.
NerdGeek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.