My problem is that I am using NavigationDrawer with multiple fragments. I have an Activity. I am trying to return from the activity to Specific Fragment. I have tried to send an intent. It works but it always stay in that fragment due to the bundle value is not null. So if you have a way how to make the bundle null or you have a better solution than the intent I will be grateful. Thanks, all.
This Method is capable of switching the Fragments
public void DisplaySelectedScreen(int id) {
infoNom = this.getIntent().getExtras();
if (infoNom != null) {
id = Integer.valueOf(infoNom.getString("Fragment"));
}
infoNom = null ; //not Working
Fragment fragment = null;
switch (id) {
case R.id.nav_home:
fragment = new Home();
break;
case R.id.nav_info:
fragment = new Informations();
break;
case R.id.nav_cmd:
fragment = new Commandes();
break;
case R.id.nav_panier:
fragment = new Cart();
break;
case R.id.nav_contact:
fragment = new Contact();
break;
case R.id.nav_about_us:
fragment = new About_Us();
break;
case R.id.nav_logout:
ParseUser.logOut();
Intent intent = new Intent(Main.this, Welcome.class);
startActivity(intent);
break;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
You can clear the bundle using below method:
getIntent().removeExtra("key");
Hope It help you !
Ways to replace extras
void removeExtra(String name)
Remove extended data from the intent.
void removeFlags(int flags)
Remove these flags from the intent.
Intent replaceExtras(Intent src)
Completely replace the extras in the Intent with the extras in the given Intent.
Intent replaceExtras(Bundle extras)
Completely replace the extras in the Intent with the given Bundle of extras.
Hope this helps
Related
i have 3 fragments inside an activity, it's a swipe menu with tabs viewpager
how to get into second fragment from another activity ?
what i mean by second fragment is when iam using this standart intent
Intent i = new Intent(getBaseContext(),Form_Mhs_04.class);
startActivity(i);
it went to first intent (case 0)
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0 :
fragment = new form_mhs_04_fragment1();
break;
case 1 :
fragment = new form_mhs_04_fragment2();
break;
case 2 :
fragment = new form_mhs_04_fragment3();
}
return fragment;
}
You can't set 2nd fragment in TabLayout in this way. You have to pass the position that you want to select using intent.
Intent i = new Intent(this, Form_Mhs_04.class);
i.putExtra("TAB_POSITION", 2);
startActivity(i);
Then inside your Form_Mhs_04 activity get this position from intent and set TabLayout selected tab based on that.
int position = getIntent().getIntExtra("TAB_POSITION", 0);
TabLayout.Tab tab = tabLayout.getTabAt(position);
if(tab != null)
tab.select();
I have looked at many of the similar question But still I'm unable to replace container layout (FrameLayout) of My Main Activity from a fragment that is visible in that container.
Here is what I'm trying to do I have a main activity with navigation drawer and On navigation drawer item click I'm changing fragment accordingly, I'm successful till this. These fragments are Login, Register, Categories, Contact Us etc. I'm changing/replacing fragments with FragmentManager, FragmentTransaction its method replace. Check the code below
public void changeFragment(int position) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment fragment = new Fragment();
BlankFragment blankFragment = new BlankFragment();
switch (position) {
case 0:
fragment = new FragMain();
break;
case 1:
fragment = new FragPrivacyPolicy();
break;
case 2:
fragment = new SettingsFragment();
break;
case 101:
fragment = new FragLogin();
break;
case 102:
fragment = new FragRegister();
break;
case 103:
fragment = new FragEditAccount();
break;
case 104:
fragment = new FragOrderHistory();
break;
case 105:
fragment = new FragForgotPass();
break;
case 106:
fragment = new FragOrderDetail();
break;
case 108:
fragment = new FragAboutUs();
break;
case 109:
fragment = new FragContactUs();
break;
case 110:
fragment = new FragMain();
break;
}
transaction.replace(R.id.flFragments, fragment);
transaction.commit();
}
Which is working fine as all my fragments extends Fragment. The problem comes when I try to call this method in one of the fragments visible in this mainActivity's container. Suppose I have a textView in FragLogin saying "Don't have an account Register" so on click this textView I have to open FragRegister in the same container of mainActivity. When I do it like this in my FragLogin App Crashes pointing to the transaction.commit();
MainActivity activity = new MainActivity();
activity.changeFragment(102);
And when I try like this
FragRegister fragRegister = new FragRegister();
// Fragment fragRegister = new FragRegister();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.Id.flFragments, fragRegister).commit();
It does not recognize the R.Id.flFragments as its inside the mainActivity not in FragLogin.
So how can I replace that FragRegister in mainActivity's container from FragLogin?
Dont use getChildFragmentManager() because it belongs to the fragment manager and the container that is holding current fragment is the activity container.Instead use getSupportFragmentManager().
Also see this stament
MainActivity activity = new MainActivity();
activity.changeFragment(102);
You are creating a new object of mainactivity which is already created.You have to call the activity method from fragments using getActivity().Call the changeFragment method like this.
((MainActivity)getActivity()).changeFragment(102);
I have this code:
public void replaceFragment(int i) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
TextView headerName = (TextView) findViewById(R.id.HeaderName);
switch (i) {
// Login Fragment
case 0:
headerName.setText("Member Login");
fragmentTransaction.replace(R.id.main_container, new
LoginActivity());
break;
// Register Fragment
case 1:
headerName.setText("Sign Up");
fragmentTransaction.replace(R.id.main_container, new RegisterFragment());
break;
}
}
My problem is on the case 0, I put the other cases as examples. My login is not a Fragment, but an Activity. So how do I manage to keep it here? With an Intent?
Instead of
fragmentTransaction.replace(R.id.main_container, new
LoginActivity());
Use
Intent inntent = new Intent(this,LoginActivity.class);
startActivity(inntent);
Just build a new intent in case 0 and start your activity instead of trying to replace a fragment with activity which is impossible unless you turn your activity to fragment
If you need both you can turn your activity to fragment and then build an empty activity to contain it when necessary
I want to pass itemId from main activity to another activity.
Now i wrote code like below, But i dont want like this, Just i want pass itemId from this activity to another when option is selected. In another activity i will write switch case option.
Here my code:
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case android.R.id.home:mDrawerToggle.onOptionsItemSelected(item);
case R.id.action_settings:SettingsFragment fragmentS = new SettingsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragmentS).commit();
break;
}
return super.onOptionsItemSelected(item);
}
Use Intents to pass data between activities.
Intent intent = new Intent(current.this, Next.class);
intent.putExtra("itemid", item.getItemId());
startActivity(intent);
And in the other activity.
String itemId= getIntent().getStringExtra("itemid");
switch(itemId)
{
...
}
From your code it seems you want to pass value from a fragment to another fragment. If so you should edit the question. Its saying you want to pass value from one activity to another. I am answering according to fragment.
Do this
switch(item.getItemId()) {
case android.R.id.home:
mDrawerToggle.onOptionsItemSelected(item);
case R.id.action_settings:
SettingsFragment fragmentS = new SettingsFragment();
Bundle args = new Bundle();
args.putInt("ItemID", item.getItemId());
fragmentS.setArguments(args);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragmentS).commit();
break;
}
In other fragment get the value like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int itemId = getArguments().getInt("ItemID");
return inflater.inflate(R.layout.fragment, container, false);
}
So, you want to pass data to a fragment actually?
Try adding before strating a fragment transaction:
Bundle bundle = new Bundle();
bundle.putInt("id", tem.getItemId());
fragmentS.setArguments(bundle);
When I press the "Back" button (tested both on emulator and Galaxy S2) the applciation closes, no matter how many fragment transactions have been commited :(
Ok, just in case, I'll post my implementation of fragment transactions although I doubt they are the problem
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
Fragment fragment;
switch(tab.getPosition()) {
case(0):
fragment = new Fragment1();
break;
case(1):
fragment = new Fragment2();
break;
case(2):
fragment = new Fragment3();
break;
default:
fragment = new Fragment();
Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_SHORT).show();
break;
}
Bundle args = new Bundle();
fragment.setArguments(args);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.okvir, fragment);
ft.addToBackStack(null);
ft.commit();
}
And also
public void searchResultsView(List<School> results) {
setResults(results);
FragmentResults searchFragment = new FragmentResults();
Bundle args = new Bundle();
searchFragment.setArguments(args);
getFragmentManager().beginTransaction().replace(R.id.okvir, searchFragment).addToBackStack(null).commit();
}
And now I'll try to provide technical details about the application so you can figure out what's holding it back (irony).
The application has one Activity, and as you could conclude, utilizes ActionBar to initialize TabHost. The ActionBar.TabHost has three tabs attached to it which correspond to Fragment1, Fragment2, Fragment3. The FragmentResults is called from Fragment2 tab. If you require additional info, let me know in the comments.
Thank you for your time and effort. Cheers
P.S. Interenstingly, when I call DialogFragment, even without AddToBackStack(null) method pressing "Back" button returns to the fragment that called it and doesn't close the app...
CHANGE THIS:
ft.replace(R.id.okvir, fragment);
ft.addToBackStack(null);
ft.commit();
TO THIS:
ft.replace(R.id.okvir, fragment,"TAGNAME");
ft.addToBackStack("TAGNAME");
ft.commit();
I WAS OMRI FROM ISRAEL HOPE ITS HELP IF U HAVE MORE PROBLAM CONTECT ME
omlrc1991#gmail.com