A repo with example: https://bitbucket.org/troffel/drawer-button-error
I've come across an issue I can't for the life of me figure out. From a much larger project I was able to deduce it into a smaller dummy project.
Problem:
When the app launches, the buttons in the drawer menu works correctly. However, after changing orientation one or more times the buttons all of a sudden require two key presses.
When logging out events, it shows that the eventlistener connected to the clicked views is triggered, however nothing seem to happen on first click.
I added the main files to the post, repo so you can replicate it if interested.
Any help is much appreciated.
Main layout:
</LinearLayout>
<LinearLayout
android:id="#+id/left_drawer"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/background_light">
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
layout of initial fragment(category_fragment):
<ListView
android:id="#+id/left_drawer_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:choiceMode="singleChoice"/>
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#ecebec"/>
<TextView
android:id="#+id/drawer_settings"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="#android:color/black"
android:textSize="20sp"
android:clickable="true"
android:onClick="openSettings"
android:gravity="center_vertical"
android:text="Settings"
android:paddingLeft="10sp"/>
</LinearLayout>
fragment that replaces the initial(preffragment):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/settings_checkbox_container"
android:layout_weight="1">
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/settings_back"
android:text="Save"
android:layout_weight="1"/>
</LinearLayout>
Mainactivity:
package com.example.testdrawer;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.left_drawer, new CategoryFragment());
fragmentTransaction.commit();
}
public void openSettings(View v){
FragmentManager fmanager = getSupportFragmentManager();
FragmentTransaction ftrans = fmanager.beginTransaction();
PrefFragment prefFragment = new PrefFragment();
ftrans.replace(R.id.left_drawer, prefFragment);
ftrans.commit();
Log.i("test", "Opening settings");
}
}
categoryfragment code:
package com.example.testdrawer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CategoryFragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.category_fragment, container, false);
//drawer menu
return v;
}
}
Preffragment.java:
package com.example.testdrawer;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class PrefFragment extends Fragment{
private Context context;
private View checkbox_container;
private Button settingsBack;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity().getApplicationContext();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.preffragment, container, false);
checkbox_container = v;
settingsBack = (Button) v.findViewById(R.id.settings_back);
settingsBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
saveSettings();
}
});
//setupSimplePreferencesScreen();
return v;
}
public void saveSettings(){
//setSettings(checkbox_container);
FragmentManager fmanager = getFragmentManager();
FragmentTransaction ftrans = fmanager.beginTransaction();
ftrans.replace(R.id.left_drawer, new CategoryFragment());
ftrans.commit();
Log.i("test", "saving settings");
}
}
OK! This is super weird, but I found the problem.
Using replace(container, fragment) instead of add(container, fragment) in my solved the issue.
The problem was that I was calling add(container, fragment) in my activitys onCreate method. This meant that when an orientation change happened, it would call add(container, fragment) on top of the already existing one.
replace(container, fragment) works because it, logically, replaces the existing one.
A solution that includes using add(container, fragment) would be to have remove(fragment) in my activitys onDestroy method, thus avoiding doable drawing.
Hope it saves someone else some time!
Related
I'm suffering through nested fragments. I have a mainactivity which calls a fragment 1 which in turns call a fragment via a button. The fragment frag2 is well instantiated but the screen is blank.
Is there something obvious with my code?
MainActivity
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager;
Frag1 f1 = new Frag1();
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame,
f1).commit();
}
}
mainactivity xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.narb.nestedfragments.MainActivity">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Fragment 1 and its xml layout:
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
/**
* A simple {#link Fragment} subclass.
*/
public class Frag1 extends android.app.Fragment {
Context context;
private Button back1;
RelativeLayout rl1;
public Frag1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = null;
rootview = inflater.inflate(R.layout.fragment_frag1, container, false);
return rootview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();
initFindView();
back1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("frag1","createdview");
//getActivity().getFragmentManager().popBackStackImmediate();
rl1.setVisibility(View.INVISIBLE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
Frag2 f2 = new Frag2();
ft.replace(R.id.fl1, f2);
ft.addToBackStack(null);
ft.commit();
}
});
}
private void initFindView(){
back1 = (Button) getActivity().findViewById(R.id.btn1);
rl1 = (RelativeLayout) getActivity().findViewById(R.id.rl1);
}
}
Is it normal that I need to make my layout frag 1 invisible before calling frag2?
and finally my fragment 2 and its layout. I see the log for frag 2 but the screen is empty:
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Frag2 extends Fragment {
Context context;
private Button btn2;
public Frag2() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview;
rootview = inflater.inflate(R.layout.fragment_frag2, container, false);
Log.i("frag2","createdview");
return rootview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();
Log.i("frag2","onactivitycreated");
btn2 = (Button) getActivity().findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("frag2","onactivitycreated");
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.narb.nestedfragments.Frag2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Test 2" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="60dp"
android:text="back"
/>
</RelativeLayout>
if your fragment 1 is having another FrameLayout where you are replacing fragment 2, then instead of getFragmentManager(), use getChildFragmentManager() in fragment 1
otherwise you are passing wrong container id in replace() method R.id.fl1 inside fragment1. you should pass R.id.content_frame there.
You have not posted the fragment1 XML Code. lets assume RelativeLayout (rl1) is your parent layout and you have FrameLayout (fl1) inside the rl1. You are making the parent layout invisible. So the fragment2 can't be visible.
If you don't want to show the fragment1 while showing there is no need for the nested fragment.
You should call like this to load second fragment.
getFragmentManager().beginTransaction()
.replace(R.id.content_frame ,new Frag2());
.addToBackStack(null);
.commit();
use content_frame id of activity_main in Frag1 class
back1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("frag1", "createdview");
//getActivity().getFragmentManager().popBackStackImmediate();
rl1.setVisibility(View.INVISIBLE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
Frag2 f2 = new Frag2();
ft.replace(R.id.content_frame, f2);
ft.addToBackStack(null);
ft.commit();
}
});
I have just started with android fragment and git stuck while implementing an example from internet.Attaching the following code below.It would be really grate to get some help.
I am getting a error at the line ft.add(R.id.fragmentone,fragment).
Regards.
MainActivity.java
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void getfragment(View view) {
FragmentOne fragment = new FragmentOne();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.add(R.id.fragmentone,fragment);
}
}
FragmentOne.java
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container,false);
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vighnesh.fragmentsexp.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonfragment"
android:layout_margin="20dp"
android:text="Click to open fragment"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragmentone"
android:layout_margin="20dp">
</fragment>
</LinearLayout>
fragment_fragment_one.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vighnesh.fragmentsexp.FragmentOne">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
Replace this code in your activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getfragment();
}
public void getfragment() {
FragmentOne fragment = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentone, fragment);
ft.commit();
}
change it.
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnFragOne = (Button)findViewById(R.id.buttonfragment);
btnFragOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentOne fragment = new FragmentOne();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.add(R.id.fragmentone,fragment); getfragment();
}
});
}
}
So use getSupportFragmentManager() instead of getFragmentManager()
Because the FragmentOne is from the support library (android.support.v4.app.Fragment) not from the framework (android.app.Fragment), you need to use android.support.v4.app.FragmentManager to do transaction with FragmentOne.
I am new to android and learning fragment instead of multiple, i have problem. when i call planet fragment from friends "java.lang.IllegalStateException: Activity has been destroyed" android runtime FATAL exception occurs here is my code. i saw this page but couldn't figure out how to use it. I will be grateful for any help
fragment_planet.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:padding="32dp" />
Friends.java
package com.app.hubara;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Friends extends Fragment {
public Friends() { }
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.content_friends, container, false);
MainActivity main = new MainActivity();
main.friends();
return rootView;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="100dp"/>
<Button
android:id="#+id/display"
android:height="70dp"
android:width="300dp"
android:layout_marginTop="0dp"
android:text="Display"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity
package com.app.hubara;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import java.util.Locale;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.display);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Friends frag_friends = new Friends();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, frag_friends);
fragmentTransaction.commit();
}
});
}
public void friends(){
PlanetFragment planetFragment = new PlanetFragment();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, planetFragment);
fragmentTransaction.commit();
}
public static class PlanetFragment extends Fragment {
public PlanetFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt("Earth");
String planet = getResources().getStringArray(R.array.planets_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", "Earth");
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
}
To access an Activity, you shouldn't do this:
MainActivity main = new MainActivity();
main.friends();
Because this destroys current Activity then instantiates another new Activity.
You can access it appropriately by calling getActivity():
MainActivity main = getActivity();
main.friends();
Note that actually you are recommended to define and use an interface to communicate between Fragment and Activity. If you want to learn further, please refer to the official training.
This question has been asked before, however, I have not found an appropriate answer for my problem. I am building a simple fragment activity that displays three buttons at the top of the screen in one container that when pressed update the fragment in the rest of the screen. Just like a tab host. I am using android.support.v4.app.Fragment in all code, but still cannot get the transaction manager to display the fragment in the container. I've been banging my head on the keyboard for hours and connot find the problem.
Here is the main activity extending FragmentActivity:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class TestUDPFragmentActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_udpfragment);
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.menu_container, new MenuFragment()).commit();
}
}
}
Here is the MenuFragment class extending fragment for my tabs:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.*;
public class MenuFragment extends Fragment{
Fragment frag;
FragmentTransaction fragTransaction;
public MenuFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frament_menu, container, false);
frag = new StringsFragment();
fragTransaction = getFragmentManager().beginTransaction().add(R.id.container, frag);
System.out.println(fragTransaction.toString());
fragTransaction.commit();
Button btnString = (Button) view.findViewById(R.id.button_string);
Button btnSlider = (Button) view.findViewById(R.id.button_slider);
Button btnTouch = (Button) view.findViewById(R.id.button_touch);
btnString.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
frag = new StringsFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
}
});
btnSlider.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
frag = new SlidersFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
}
});
btnTouch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
frag = new TouchFragment();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
}
});
return view;
}
}
Here is one of the three fragments that I can't get to display:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.support.v4.app.*;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class StringsFragment extends Fragment {
public StringsFragment() {
}
public View onCreatView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_strings, null);
return rootView;
}
}
activity_test_udpfragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/menu_container"
android:background="#eee"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="#+id/container"
android:background="#aaa"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
Here is frament_menu:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="android.att/buttonBarStyle" >``
<Button
android:id="#+id/button_string"
style="android.att/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="String" />
<Button
android:id="#+id/button_slider"
style="android.att/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Slider" />
<Button
android:id="#+id/button_touch"
style="android.att/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Touch" />
</LinearLayout>
And here is fragment_strings:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:scaleType = "centerCrop"
android:src="#drawable/me_hooded" />
</RelativeLayout>
Any help is greatly appreciated!
EDIT:
Here is new TestUDPActivityFragment with buttons to change fragment:
package com.example.testudpfragment;
import com.example.testudp.R;
import android.support.v4.app.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.Bundle;
public class TestUDPFragmentActivity extends FragmentActivity {
Fragment frag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_udpfragment);
Button btnString = (Button)findViewById(R.id.btn_string);
Button btnSlider = (Button)findViewById(R.id.btn_slider);
Button btnTouch = (Button)findViewById(R.id.btn_touch);
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new StringsFragment()).commit();
btnString.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frag = new StringsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
}
});
btnSlider.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frag = new SlidersFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
}
});
btnTouch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frag = new TouchFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
}
});
}
}
}
I can see a couple of problems...
First, your FrameLayout containers are defined in your activity_test_udpfragment.xml layout file which is inflated by your TestUDPFragmentActivity. The problem with attempting to create / add your StringsFragment in your MenuFragment code using the following...
fragTransaction = getFragmentManager().beginTransaction().add(R.id.container, frag);
...is the MenuFragment inflates a layout from your frament_menu.xml which doesn't contain a FrameLayout with id of R.id.container.
That's your first problem but the second is with attempting to use a Fragment to create / add a Fragment in the first place. Your MenuFragment is not able to use the Framelayout in the TestUDPFragmentActivity.
In short, I suspect you want both Fragments to appear in your Activity layout (and not have the StringsFragment as a 'child' of MenuFragment) in which case you need to put all code for creating / adding both Fragments into your Activity.
I have 2 fragments . In the first fragment there is a button which on clicking will move to 2 nd fragment. When the screen is rotated in this 2nd fragment,after orientation change I see first fragment instead of 2nd fragment. Can anyone help me in solving this issue?
The code is as below :
Activity code:
package com.andr.fragmentsorientationdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
public class OrientationActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_orientation);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, new Fragmentone()).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.orientation, menu);
return true;
}
}
<br>Code for FragmentOne <br>
=============================
package com.andr.fragmentsorientationdemo;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class Fragmentone extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, container, false);
Button b = (Button)v.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, new Fragmenttwo()).addToBackStack(null).commit();
}
});
return v;
}
}
<br>code for Fragmenttwo
===================
package com.andr.fragmentsorientationdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* #author 245742
*
*/
public class Fragmenttwo extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_two, container, false);
Button b = (Button)v.findViewById(R.id.button1);
return v;
}
}
<br>layout file for fragmentone <br>
=====================================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<br> Layout for fragmenttwo
==============================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2222222222" />
</LinearLayout>
Can anyone help me in solving this issue.
Try this-
FragmentManager fragmentManager =null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_orientation);
if(fragmentManager==null){
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, new Fragmentone()).commit();
}
}
or else
use this in manifest's activity tag-
android:configChanges="orientation"