public class MainActivity extends FragmentActivity {
FragmentTransaction ft;
Button b1, b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
ft.replace(android.R.id.content, new Fragment1());
} else {
ft.add(R.id.LinearLayout1, new MainFragment());
ft.add(R.id.LinearLayout2, new Fragment1());
}
ft.commit();
}
}
public class MainFragment extends Fragment implements OnClickListener{
Button b1,b2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.activity_main_fragment, container, false);
}
#Override
public void onStart() {
super.onStart();
// ---Button view---
b1 = (Button) getActivity().findViewById(R.id.f1);
b2 = (Button) getActivity().findViewById(R.id.f2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==b1){
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.LinearLayout2,new Fragment2());
ft.commit();
}
if(v==b2){
FragmentManager fm =getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.LinearLayout2,new Fragment3());
ft.commit();
}
}
}
The problem here when the application is running in portrait mode the Fragment1 appear as i want and when i change the orientation to landscape ,the MainFragment and Fragment2 appear as i want too but Fragment1 still there,
and when i change orientation again to portrait ,the application stopped.
How can I solve it?
So the problem is you are assuming that every time onCreate is called you are starting out with no fragments...which is not the case.
The first time onCreate is called for MainActivity there will be no fragments. Assuming the device was in portrait, Fragment1 will be added. When you rotate to landscape then another instance of Fragment1 will be added and an instance of MainFragment. You now have three fragment instances. When you go back to portrait its going to want to add ANOTHER instance of Fragment1...but it is probably crashing because the portrait layout doesn't have R.id.LinearLayout1 or R.id.LinearLayout2.
Bottom line is...you need to remove fragments once you are done with them...they don't just go away by themselves.
Related
public class MainActivity extends AppCompatActivity {
Context context;
LinearLayout menuClcick,gallerClcik,eventsClick;
LayoutInflater inflater;`enter code here`
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//we don't need to set view, our fragment will handle it
setPointer();
//Fragment Manger
FragmentManager fm = getFragmentManager();
//create instance of Fragment Transaction to handle fragment replace and animation
FragmentTransaction ft=fm.beginTransaction();
int displayMode = getResources().getConfiguration().orientation;
Log.e("WTF", "onCreate: "+displayMode );
//choose which fragment to display according to screen orientation
if (displayMode==1) //portrait
{
// that's the Fragment that I use to display a layout in the portrait and other layout in the landscape//
//create instance of our portrait fragment
Fragment1 f1=new Fragment1();
//change content of the screen to our new fragment
ft.replace(android.R.id.content,f1);
}
else
{
Fragment2 f2=new Fragment2();
ft.replace(android.R.id.content,f2);
}
//choose animation
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
//commit our changes
ft.commit();
}
private void setPointer() {
this.context=this;
menuClcick=findViewById(R.id.menuClick);
gallerClcik=findViewById(R.id.gallerClcik);
eventsClick=findViewById(R.id.eventsClick);
//this is the problem the app have no problem to find the buttons but it stops working when I try to put onclick listener in it//
menuClcick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "portrait", Toast.LENGTH_SHORT).show();
}
});
}
You are trying to access views that are not yet created,
As the documentation lifecycle shows, you should implement onCreateView() to inflate your layout and only there you have access to your R.id.menuClick.
So basically, you should call your setPointer() method on onCreateView().
I am making a simple demo project using Fragments, in which i am calling SecondFragment from FirstFragment on button click.
And i called SecondFragment without any issue, but i getting view of both the Fragments SecondFragment and FirstFragment
So where i am doing mistake ?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new FirstFragment()).commit();
}
}
public static class FirstFragment extends Fragment {
Button buttonCallSecondFragment;
public FirstFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first, container,
false);
buttonCallSecondFragment = (Button) rootView.findViewById(R.id.button1);
buttonCallSecondFragment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
SecondFragment fragment = new SecondFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
ft.commit();
}
});
return rootView;
}
}
public static class SecondFragment extends Fragment {
public SecondFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second, container,
false);
return rootView;
}
}
}
You need to remove the first fragment, you can do that either by usingreplace or first calling remove then add
To be able to press the back button add the transaction to the back stack,you do that by calling addToBackStack(tag) on your fragment manager. Tag may be null.
You are adding fragment on already displaying fragment in your android app.
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
ft.commit();
Do not add fragment but replace fragment when already one fragment is loaded on activity.
So for implementing that :
Please add your code in OnCreate() and add below code to your click listener :
FragmentManager fm = getFragmentManager();
SecondFragment fragment = new SecondFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack(null);
ft.commit();
Thank you.!!
if you want to replace fragment you should call replace in place of add :
buttonCallSecondFragment = (Button) rootView.findViewById(R.id.button1);
buttonCallSecondFragment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
SecondFragment fragment = new SecondFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
});
You are adding the fragment so it will come on top of another fragment give background color to rootview of scecond fragment
you can also load that as Nested fragment
public void addFragB() {
FragmentManager childFragMan = getChildFragmentManager();
FragmentTransaction childFragTrans = childFragMan.beginTransaction();
SecondFragment 2ndfrag = new SecondFragment();
childFragTrans.add(R.id.fragA_LinearLayout, 2ndfrag);
//childFragTrans.addToBackStack("");
childFragTrans.commit();
}
No, you cannot communicate between fragments like this. You need to communicate between fragments via the container activity.
I suggest, you make a navigation method in your activity and switch/call between fragments from there.
Below is a small snippet:
In your container activity:
Fragment fragment;
public void navigateTo(Fragment newFragment,boolean addToBackStack) {
this.fragment = newFragment;
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.fragment_container, newFragment);
if(addToBackStack)
ft.addToBackStack(fragment.getClass().getSimpleName());
ft.commit();
}
If you are going from Fragment1 to Fragment2 then in your Fragment1 do the following:
((YourContainerActivity) getActivity()).navigateTo(new Fragment2(),false); //if you want to add to back stack make 2nd argument true.
private void selectItem(int position) {
selectedPosition = position;
// update the main content by replacing fragments
fragment = null;
fragmentStack = new Stack<Fragment>();
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
}
//redirect the screen
if (fragment != null) {
fragmentManager = getSupportFragmentManager();
redirectScreen(fragment);
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(title[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
i have an activity like WelcomeActivity and a fragment like GuideFragment.
i have a button in WelcomeActivity which cause a navigation to GuideFragment. Also i have a button in GuideFragment which navigates back to the WelcomeActivity.
For first time it goes to GuideFragment by clicking on the button and cames back to WelcomeActivity by pressing btn_back. Hwever after coming back to activity, the button does not work any more.
whats wrong with my code?
activity clas:
public class WelcomeActivity extends FragmentActivity
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
GuideFragment guideFragment = new GuideFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
guideFragment = new GuideFragment();
}
public void onGuideClick(View view)
{
fragmentTransaction.replace(android.R.id.content, guideFragment);
fragmentTransaction.addToBackStack(null);
fragmentManager.popBackStack();
fragmentTransaction.commit();
}
}
and fragment class:
public class GuideFragment extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// create ContextThemeWrapper from the original Activity Context with the custom theme
Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.guideFrag);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
// inflate the layout using the cloned inflater, not default inflater
View view = inflater.inflate(R.layout.fragment_guide, container, false);
Button button = (Button) view.findViewById(R.id.btn_back);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
getActivity().getFragmentManager ().popBackStack();
}
});
return view;
}
}
Thanks
Remove fragmentManager.popBackStack(); from onGuideClick(View view). Also the transaction should not be a member field.
public void onGuideClick(View view) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, guideFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
You also initiate the GuideFragment twice, once is enough.
I changed WelcomeActivity like this:
public class WelcomeActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
public void onGuideClick(View view)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
GuideFragment guideFragment = new GuideFragment();
fragmentTransaction.replace(android.R.id.content, guideFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
I have a Activity with four buttons at the bottom like tabs. By pressing any button a new Fragment is displayed in the FrameLayout above these buttons like we do in TabActivity. See My Problem here .Now i think i should find a way to hide and show those fragments. Kindly tell me how can i show and hide a fragment without reloading it again.
Main Purpose of showing hiding a fragment is to maintain its current state. In one of my fragment i have an AsyncTask whenever i switch between fragment it call that AsynTask again.
// to show fragment when it is hidden
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.show(fragment1)
.commit();
// to hide fragment
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.hide(fragment1)
.commit();
you can't pass by some view like
declare 4 frameLayout
private FrameLayout fragment1;
private FrameLayout fragment2;
private FrameLayout fragment3;
private FrameLayout fragment4;
and
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, Fragment1.newInstance());
ft.replace(R.id.fragment2, Fragment2.newInstance());
ft.replace(R.id.fragment3, Fragment3.newInstance());
ft.replace(R.id.fragment4, Fragment4.newInstance());
ft.commit();
and play with visible or gone ? like
fragment1.setVisibility(View.Visible);
fragment2.setVisibility(View.gone);
fragment3.setVisibility(View.gone);
fragment4.setVisibility(View.gone);
and by the way : works for me ->
public class Activity extends ActionBarActivity {
private FrameLayout fragment1;
private FrameLayout fragment2;
private Button bt1;
private Button bt2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frg_test_frg);
fragment1 = (FrameLayout) findViewById(R.id.fragment1);
fragment2 = (FrameLayout) findViewById(R.id.fragment2);
bt1 = (Button) findViewById(R.id.button);
bt2 = (Button) findViewById(R.id.button2);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fragment1.setVisibility(View.VISIBLE);
fragment2.setVisibility(View.GONE);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fragment1.setVisibility(View.GONE);
fragment2.setVisibility(View.VISIBLE);
}
});
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, new Fragment1());
ft.replace(R.id.fragment2, new Fragment2());
ft.commit();
}
I am useing This Sort of Code To Handle Three Fragment in Main Activity ...
FragmentA is Fixed it One Frame .. I change FragmentB and FragmentC on Button Click on FragmentA.
his Code is Running well either in Portrait or Landscape view .Here is The Code bellow.
public class MainActivity extends FragmentActivity implements
OnSwitchClickListener {
FragmentManager manager;
FragmentA fragA;
FragmentB fragB;
FragmentC fragC;
boolean fragBSet = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragA = new FragmentA();
fragB = new FragmentB();
fragC = new FragmentC();
manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.add(R.id.a_container, fragA, "frag A");
if (fragBSet) {
ft.add(R.id.bc_container, fragC, "frag C");
} else {
ft.add(R.id.bc_container, fragB, "frag B");
}
fragBSet = true;
ft.commit();
}
#Override
public void onSwitchClick(View v) {
Toast.makeText(getApplicationContext(), "Switch clkick from Activity",
Toast.LENGTH_LONG).show();
FragmentTransaction ft = manager.beginTransaction();
if (fragBSet) {
ft.remove(fragB);
ft.add(R.id.bc_container, fragC, "frag C");
fragBSet = false;
} else {
ft.remove(fragC);
ft.add(R.id.bc_container, fragB, "frag B");
fragBSet = true;
}
ft.commit();
}
This Code is Running well either in Portrait or Landscape view ... But When Ever I change the Orientation The Two Fragments Override One Another.
Need Solution.
When your orientation change, your Activity is recreated.
"In the scenario where a user rotates their device, Android will destroy your
application’s activity(s). Before destroying them it calls, onSaveInstanceState,
allowing developers to persist data. Once the activity is recreated post
rotation, the OS will call onRestoreInstanceState giving developers a chance to
restore the application state pre-rotation."
You should try to save your Fragments State.
putFragment: Put a reference to a fragment in a Bundle. This Bundle can be persisted as saved state, and when later restoring getFragment(Bundle, String) will return the current instance of the same fragment.
#Override
protected void onSaveInstanceState(Bundle outState) {
FragmentManager manager = getFragmentManager();
manager.putFragment(outState, MyFragment.TAG, mMyFragment);
}
getFragment: Retrieve the current Fragment instance for a reference previously placed with putFragment(Bundle, String, Fragment).
private void instantiateFragments(Bundle inState) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (inState != null) {
mMyFragment = (MyFragment) manager.getFragment(inState, MyFragment.TAG);
} else {
mMyFragment = new MyFragment();
transaction.add(R.id.fragment, mMyFragment, MyFragment.TAG);
transaction.commit();
}
}
restoreFragment:
#Override
protected void onRestoreInstanceState(Bundle inState) {
instantiateFragments(inState);
}
Hope this helps.