I have navigation bar on my app. Using that has created fragments. I added regular activities to the app and I have found how to go from a Fragment page to an activity page with using the onclick method.
By the click of a button I want to go from an activity page to a fragment page. How do I go about that?
Thank you
apply on click listner on the button and apply below code :-
Fragment fragment = new VehiclesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment).commit();
If you want to close the current activity to go back to your Fragment-container Activity you may call the finish() method or onBackPressed() method (in case you're not overriding it).
If you want to bring a fragment to the current activity you must have a fragment container and attach it with:
getSupportFragmentManager().beginTransaction.add(R.id.container, fragment, TAG).addToBackStack().commit();
Try this code..
btn.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),FragmentTest.class);
getActivity().startActivity(intent);
}
});
If you want to move around in fragment then you have to be using either navigation bar clicks on tabs or use this from one fragment to another on a click of button .. like this
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = Fragment.instantiate(MainActivity.this, <name of the fragment>);
Bundle bundle = new Bundle();
bundle.putBoolean(ALL_EVENTS, true);
fragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
Simply use this:
Intent(view.context, DestinationActivity::class.java).also {
view.context.startActivity(it)
}
Related
I Tried the following code to move from a Fragment extends Fragment to an Activity extends activity.
Intent mIntent = new Intent(getActivity(), UserProfile.class);
Bundle bundle = new Bundle();
bundle.putBoolean(Constant.isFromChatScreen, false);
mIntent.putExtras(bundle);
startActivity(mIntent);
In BaseActivity.java I have Fragment A, Fragment B, Fragment C..from Fragment A I moved to Userprofile.java now I want to move back to Fragment A onBackPressed
How can I do that?
Check Google documentation for findFragmentByTag() here.
You can also use addToBackStack() while calling the fragment from FragmentTransaction
Just use addToBackStack(null) before commit when you make fragment transactions and then when you press the back button you will get back to your fragment.
Old question, but anyone looking for an answer to this can just do this:
#Override
public void onBackPressed() {
finish();
}
finish() "closes" the current Activity and takes you back to the Fragment (Actually, it takes you back to the Activity hosting the Fragment)
I want to launch a new fragment to view some data. Currently, I have a main activity that has a bunch of actionbar tabs, each of which is a fragment. So, within a tab fragment, I have a button, chartsButton. I have my onclicklistener all set for it, and here's the onClick method:
public OnClickListener chartsListener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent chartFragment = new Intent();
startActivity(chartFragment);
}
};
Now, as I said, this listener is within a class that extends Fragment. So, I want to launch a new fragment (chartsFragment) via the intent to replace the whole screen. When the user clicks back, it'll bring them back to the tabs and main activity. Here's my chart fragment:
public class chartsFragment extends Fragment {
public View onCreateView() {
//LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return (inflater.inflate(R.layout.chartfragment, null));
}
}
The current error I am dealing with: "android.content.ActivityNotFoundException: No Activity found to handle Intent { }". That's fine, I understand that I could use getActivity().startActivity(chartsFragment), but that results in the same error. I suppose what I am looking for here, is how do I launch an intent from within a fragment that results in opening a new fragment?
The answer to your problem is easy: replace the current Fragment with the new Fragment and push transaction onto the backstack. This preserves back button behaviour...
Creating a new Activity really defeats the whole purpose to use fragments anyway...very counter productive.
#Override
public void onClick(View v) {
// Create new fragment and transaction
Fragment newFragment = new chartsFragment();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
http://developer.android.com/guide/components/fragments.html#Transactions
You cannot open new fragments. Fragments need to be always hosted by an activity. If the fragment is in the same activity (eg tabs) then the back key navigation is going to be tricky I am assuming that you want to open a new screen with that fragment.
So you would simply create a new activity and put the new fragment in there. That activity would then react to the intent either explicitly via the activity class or implicitly via intent filters.
Try this it may help you:
public void ButtonClick(View view) {
Fragment mFragment = new YourNextFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mFragment).commit();
}
Try this it may help you:
private void changeFragment(Fragment targetFragment){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_fragment, targetFragment, "fragment")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
try this:
in SecondActivity:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra("openFragment", "YourFragment");
startActivity(intent);
}
});
In the FisrtActivity that contains the fragment:
if (getIntent().getStringExtra("openFragment").equals("YourFragment")){
getSupportFragmentManager().beginTransaction().replace(R.id.containerView, new YourFragment()).commit();
}
I am developing an android in which when I pressed device back button I go to previous fragment. What I want to achieve is that when I pressed device back button I don't want to go to previous fragment. How can I achieve that?.
You must be doing calling addtobackstack("name") while adding or replacing the fragment.Remove this function before calling next fragment you wont go back to previous fragment for further desc
Do not add that fragment to backstack of the new fragment you're calling. Look at the below example I've used to explain you how this works.
To make that fragment come again, just add that fragment to backstack which you want to come on back pressed,
Eg:
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new LoginFragment();
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = ((FragmentActivity)getContext()).getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack("SignupFragment");
ft.commit();
}
}
});
In the above case, I m opening a LoginFragment when signIn button is pressed, right now am in SignupFragment. So if I call addToBackStack(TAG), TAG = "SignupFragment", then when back button is pressed in LoginFragment, we come to SignUpFragment.
Happy Coding!
I know how to add a Fragment to the backstack but how do I know, when the user presses the back Button, which fragment I left and which I went to? I need to do a certain action depending on this so I need to know from and to which fragment I am going. Specifically I need to know which fragment I left so if it is a certain fragment, I can remove a button.
Override onBackPressed in Activity:
#Override
public void onBackPressed(){
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentById(R.id.content_frame); // get the fragment that is currently loaded in placeholder
Object tag = f.getTag();
// do handling with help of tag here
// call super method
super.onBackPressed();
}
You can add the Fragment like this :
ArticleMain articalemain = new ArticleMain();
getFragmentManager().beginTransaction().add(R.id.fragment_Top, articalemain, "MY_FRAGMENT").commit();
While Removing the Fragments do like this :
ArticleMain myFragment = (ArticleMain) getFragmentManager()
.findFragmentByTag("MY_FRAGMENT");
if (myFragment.isVisible()) {
// add your code here
myFragment.getFragmentManager().beginTransaction()
.remove(myFragment).commit();
}
I want to launch a new fragment to view some data. Currently, I have a main activity that has a bunch of actionbar tabs, each of which is a fragment. So, within a tab fragment, I have a button, chartsButton. I have my onclicklistener all set for it, and here's the onClick method:
public OnClickListener chartsListener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent chartFragment = new Intent();
startActivity(chartFragment);
}
};
Now, as I said, this listener is within a class that extends Fragment. So, I want to launch a new fragment (chartsFragment) via the intent to replace the whole screen. When the user clicks back, it'll bring them back to the tabs and main activity. Here's my chart fragment:
public class chartsFragment extends Fragment {
public View onCreateView() {
//LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return (inflater.inflate(R.layout.chartfragment, null));
}
}
The current error I am dealing with: "android.content.ActivityNotFoundException: No Activity found to handle Intent { }". That's fine, I understand that I could use getActivity().startActivity(chartsFragment), but that results in the same error. I suppose what I am looking for here, is how do I launch an intent from within a fragment that results in opening a new fragment?
The answer to your problem is easy: replace the current Fragment with the new Fragment and push transaction onto the backstack. This preserves back button behaviour...
Creating a new Activity really defeats the whole purpose to use fragments anyway...very counter productive.
#Override
public void onClick(View v) {
// Create new fragment and transaction
Fragment newFragment = new chartsFragment();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
http://developer.android.com/guide/components/fragments.html#Transactions
You cannot open new fragments. Fragments need to be always hosted by an activity. If the fragment is in the same activity (eg tabs) then the back key navigation is going to be tricky I am assuming that you want to open a new screen with that fragment.
So you would simply create a new activity and put the new fragment in there. That activity would then react to the intent either explicitly via the activity class or implicitly via intent filters.
Try this it may help you:
public void ButtonClick(View view) {
Fragment mFragment = new YourNextFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mFragment).commit();
}
Try this it may help you:
private void changeFragment(Fragment targetFragment){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_fragment, targetFragment, "fragment")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
try this:
in SecondActivity:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra("openFragment", "YourFragment");
startActivity(intent);
}
});
In the FisrtActivity that contains the fragment:
if (getIntent().getStringExtra("openFragment").equals("YourFragment")){
getSupportFragmentManager().beginTransaction().replace(R.id.containerView, new YourFragment()).commit();
}