I installed Eclipse on a new machine and it keeps creating fragment_main.xml when I create a new Android Application Project. How can I prevent Eclipse from doing this, since I don't need a fragment_main.xml? I'd rather only have the activity_main.xml on start.
1.Creat project normally.
2.Copy fragment_main.xml to activity_main.xml (content). Then delete fragment_main.xml
3.In MainActivity.java delete the following content :
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
and
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
enjoy it.
There's currently no way (as far as I know) to keep Android from creating a fragment when you create an Activity. You can uncheck the "Create Activity" check box and create a new Activity, but that's really all you can do.
Anyways, Fragments are good for Phone/Tablet Compatibility, and should be used in almost all scenarios except for very basic uses.
Related
what am I doing wrong... ;-)
In the tutorial, I'm supposed to "Create the second activity", the screenshot of the tutorial shows how to do it in Eclipse, but I'm using AS. Anyway, I've added a new blank activity from within Android Studio and the generated DisplayMessageActivity.java file looks identical with the one in the tutorial.
Then the tutorial says to add the following code in the onCreate method:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Q1: here the word 'container' in R.id.container is marked in red in AS; that is, can not resolve symbol 'container'. The tutorial doesn't say anything about it and I haven't got a clue how to declare it. What is it?
Further down in the DisplayMessageActivity I'm supposed to add:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
return rootView;
}
}
Q2: What on earth is R.layout.fragment_display_message? All I've got is R.layout.activity_display_message.
Any advice is appreciated, thank you!
here the word 'container' in R.id.container is marked in red in AS; that is, can not resolve symbol 'container'.
That is because you did not have a container id layout in your activity_display_message.xml to be displayed when fragment is inflated/created.
sample:
in your activity_display_message
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container" <--- that is the id you must have --->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >
</RelativeLayout>
.
What on earth is R.layout.fragment_display_message??
it is the layout for your fragment so when it is inflated it will add to the top of your activity_main layout.
solution:
create a layout in your layout folder named fragment_display_message.xml
Replace
View rootView = inflater.inflate(R.layout.fragment_display_message,container, false);
with
View rootView = inflater.inflate(1,container, false);
When ever i create a new project, the Fragment_main.xml file is added to my Layout folder and unlike in Eclipse it is this file that contains what is normally in the Activity_Main.xml file.Why is the Fragment_main.xml file always added to my projects in Android Studio and how is it different from the "regular" Activity_main.xml file?
The Activity_main.xml contains the Layout for the FragmentActivity and the fragment_main.xml is the Layout for the fragment.
For more information to fragments and how you can use it.
Visit: http://developer.android.com/training/basics/fragments/index.html
If you are creating a new Project and it adds fragment_main.xml by default you must be selecting a layout by default. Maybe a pager/spinner layout?
Fragment_main is the same as activity_main. The names are just string labels and mean nothing in and of itself and are just changed for clarity by the IDE.
Have a read of this.
http://developer.android.com/guide/topics/ui/declaring-layout.html
just as Bytehawks said above.
activity_main.xml describes Layout for the FragmentActivity and the fragment_main.xml is the Layout for the fragment.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //get the activity_main.xml for layout
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//code for describing layout more details, get fragment_main.xml
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
I am using several fragments to be dynamically added into activity. Everything works fine, when I press back-button, the fragments go to backstack. And when I resume it, it appears. But everytime on Resume, it is recreating the fragment and call onCreateView. I know it is a normal behavior of the fragment lifecycle.
This is my onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.competitive_programming_exercise, container, false);
return rootView;
}
I want to stop those fragments from recreating. I tried with onSavedInstanstate but nothing is working. How can I accomplish that?
In the Activity's onCreateView set the savedInstanceState to null before calling the super method. You could also remove only the keys "android:viewHierarchyState" and "android:fragments" from the savedInstanceState bundle. Here is code for the simple solution, nulling the state:
#Override
public void onCreate(Bundle savedInstanceState)
{
savedInstanceState = null;
super.onCreate(savedInstanceState);
...
}
Iam using 5 fragments and working for me good as I was facing the same issue before..
public class MyFragmentView1 extends Fragment {
View v;
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
if (v == null)
v = inflater.inflate(R.layout.my_fragment_view_layout,
container, false
);
return v;
}
}
I put the view variable inside class and inflating it as new only if the view instance is null or otherwise use the one created before
You can't stop the fragment from being recreated, unfortunately. The best you can do is to remove the fragment in a transaction, after it has been restored but before it gets displayed.
If you know you are going to remove the fragment immediately you can reduce the performance hit of restoring the fragment by simplifying methods such as onCreateView() to return a dummy view, rather than inflating the whole view hierarchy again.
Unfortunately the tricky part is finding the best place to commit this transaction. According to this article there are not many safe places. Perhaps you can try inside FragmentActivity.onResumeFragments() or possibly Fragment.onResume().
Just created a new project in the newest ADT release in Eclipse and found that it will setup up certain environments for you to get things started. I choose Tabs + Swipe.
It has this code I have question on:
public static class DummyFragment extends Fragment {
public DummyFragment () {
}
public static final String ARG_SECTION_NUMBER = "section_number";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
Both tabs refer to this same fragment. All it does is switch the content on the TextView that has the tab position number on it (1,2, or 3).
The more advanced question first: I want two different Fragments that the tab switches to. In the example code, it points to the same fragment. Where does this change take place? and can I see brief code example?
Easier question: I have two pre-defined XML layouts I'd like to set each tab (or Fragment) with. Do I do this in the actual Fragment? And if so, where? setContentView does't seem to be working in the onCreateView method?
Not really sure what this question is asking exactly, but if I understand correctly the TabHost (or whatever you are using to manage the Fragment tabs) is instantiating multiple instances of the DummyFragment and then attaching each to the screen when a tab is clicked. This is all done behind the scenes... all you need to worry about is implementing the Fragment and telling the TabHost when it should be instantiated/displayed.
Fragments don't have a setContentView method. You should inflate your Fragment's layout from xml in onCreateView. For instance with,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}
Hi i have set up a view pager and fragments for each page and that's all working as it should however i can not get a preference screen to show on one of my pages no matter what i try.
My fragment that i'm trying to add a preference screen looks like this
public class Extras extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.extras, null, false);
return V;
}
}
So then i read the documentation on PreferenceFragments here
http://developer.android.com/reference/android/preference/PreferenceFragment.html
But when i add this in my Extras class as the above link shows
public static class Extras extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
I error out just extending PreferenceFragment errors in my view pager i read the below link though that PreferenceFragments aren't part of the compatiblity library which i believe is my issue. So is it possible to add a preference screen in one of my pages in my viewpager or is this something i'm going to have to do with a listview and basically make my own preference screen
Was PreferenceFragment intentionally excluded from the compatibility package?
Thank you for any help with this issue
Never got the answer i was looking for so i ended up going with a listview instead
I posted my solution here http://forum.xda-developers.com/showthread.php?t=1363906
I simply rebuilt the hidden Preference* class via Reflection.