I already create my application with Navigation Drawer. I want to start my second activity and keep the navigation drawer.
When, i opened my second activity using this code:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
The Navigation Drawer disapper.
So, how can i do this?
Try these code :-
we need to call fragment that's why we will use these code
FragmentTransaction ftHome =
getActivity().getSupportFragmentManager().beginTransaction();
Fragment fmHome = new HomeFragment();
ftHome.replace(R.id.container_body, fmHome);
ftHome.commit();
Related
So I have Activity A with Fragment A.1, and I also have Activity B with Fragment B.1.
What I want to ask is, how do I move directly from Fragment A.1 to Fragment B.1?
I know to move from Fragment A.1 to Activity B, is by:
Intent i = new Intent (getActivity (), MainActivity.class);
startActivity (i);
getActivity ().finish();
But how to move straight to Fragment B.1?
Each Activity A and Activity B has a different <FrameLayout> for Fragment replacement
UPDATE 1.0
I've tried my own way and also the way #cewaphi answered with code like this,
In Activity A:
Intent i = new Intent(TransactionDone.this, MainActivity.class);
i.putExtra("immediatelyTransactionToFragment", true);
startActivity(i);
finishAffinity();
In Activity B:
boolean shouldTransitionToFragment = getIntent().getBooleanExtra("immediatelyTransationToFragment", true);
if (shouldTransitionToFragment) {
Fragment fragment = new Wallet();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFrameLayout, fragment);
transaction.commit();
Log.d("DEBUGGING REDIRECT", "Go to Fragment B.1");
}
The log "Go to Fragment B.1" was created but the transaction doesn't work
When using a single activity and e.g. using the navigation component is not an option.
Consider the following:
In your fragment A.1 when starting the activity store a Boolean
i.putExtra("immediatelyTransationToFragment", true);
In activity B
shouldTransitionToFragment = getIntent().getBooleanExtra("immediatelyTransationToFragment");
// after activity was created
if (shouldTransitionToFragment) {
// Execute the transition action as you would when pressing the button
}
Update 2020/09/07
You are trying to transition to a Fragment from your Activity like this:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFrameLayout, fragment);
transaction.commit();
The documentation states that you should first add the fragment to the activity:
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Apparently your transition works when you click your button. Are you doing it the same?
But I assume at that time the activity has already been created.
Try once to add your fragment instead of replace. I don't know how your container is initialised but adding might be the operation you want, I refer to this good answer for clarification.
Also consider to perform to call this transaction after your activity was created.
I don't know how to express this, but the idea was I have a Navigation Drawer that will call (Intent an Activity) and that Activity will automatically replace the content base from the NavigationView link click.
So from my MainActivity -> ReadActivity (Activity call other Activity) and replace the ReadActivity content with the topic click from the NavigationView .
This code I use for replacing:
LayeringViewer layeringViewer = new LayeringViewer();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.layer_frame_layout,layeringViewer);
ft.commit();
I know how to place a fragment. But how can I do this while calling other Activity? Passing the Fragments like Bundles.
if you want to change the content according to selection from drawer, then pass data on drawer close to activity and according to that replace fragments
Ex:
Fragment fragment = new SupportFragment();
Bundle args = new Bundle();
args.putInt(SupportFragment.ARG_NAME, name);
args.putString("screenfrom", screenfrom);
fragment.setArguments(args);
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
You cannot pass fragments in bundles, you can however send extras with your intent and in the receiving activity get the intent and replace the fragment container with a fragment based on the value of the intent.
There are similar questions, but none of the are solving my issue.
My app flow is following:
Activity home starts Activity B(which does the setup work)
In activity B hosts three screens as fragments...
Fragment1 -> fragment2-> fragment 3.
This is how I make fragments. I am not using replace.just adding it.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = null;
if (getFragType().equals("simple")) {
fragment = new SimpleFragment().newInstance();
}
if (getFragType.equals("inter")) {
if (getFragType.getComponent().equals("con"))
fragment = new SetupConFragment().newInstance();
else if (getFragType.getComponent().equals("ben"))
fragment = new SetupBenageFragment().newInstance();
}
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
Fragment3 has a done button. So once all the 3 steps are done from fragment 3 am launching a new activity C.
The issue is:--
From the activity C , if the user presses the back button, it diplays fragment 2, then fragment 1. Ideally once the user is in Activity C, back press should just exit the screen
I have used all combinations of the intent flags, but it is not helping.
And this is on my button click.
Intent launch = new Intent(mContext, MainActivity.class);
launch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launch.putExtra("Exit me", true);
mContext.startActivity(launch);
((ConfigureActivity)mContext).finish();
Any help will be appreciated.
You can do this way:
Clear Fragment stack:
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
Clear Activity stack:
Intent intent = new Intent(Your_Current_Activity.this, Your_Destination_Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Hope this will help you.
Say, if you want to start Activity B in Activity A but you don't want user to go back to Activity A,
final Intent intent = new Intent(this, ActivityB.class);
startActivity();
finish(); // kill the current activity (i.e., Activity A) so user cannot go back
In your Activity C, you can control Back key by overriding onBackPressed().
#Override
public void onBackPressed()
{
// code here depending on your needs
}
My current activity contains fragments. I want to open new Activity on button click from current activity. New Activity is transparent, and because of that I have to hide fragment from prev Activity. No buttons from prev activity should be shown under new Activity's buttons. I've done that. It works. Only problem is because there is little pause between switching these two Activities. Obviusly hiding fragment of prev activity takes time. Can I make it hide fragment faster? Here is my code:
#Override
public void onArcadeButtonClicked() {
fragmentManager= getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.hide(fragments[MAINMENU]);
transaction.commit();
Intent i= new Intent(this,ArcadeMapActivity.class);
startActivity(i);
}
When i remove first four lines and only call new Intent there is no pause, new Activity is shown immediately, but fragment from old Activity stays visible under new Activity. Any idea how to solve this problem?
I have implemented navigation drawer in my app. When I open the app first it shows blank screen. Instead I want it to start fragmentA.
I have tried
Intent i = new Intent(MainActivity.this, fragmentA.class);
startActivity(i);
but this gives me activity not found exception.
So how can I start a fragment inside an activity?
Try:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.yourFrame, fragmentA.getInstance());
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
in your activity