Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADT blank activity created with fragment activity

I am really confused right now because whenever I create a new Android app with blank activity it always comes out with fragment_main.xml. I just wanted to create a blank activity without the fragment one.

In the first image the blank activity comes with the fragment layout:

first image blank activity

The second image shows the created fragment_main

second image blank activity

Now I am really confused... this only happened after updating ADT to the latest version. I have referred to this thread: Adt doesn't create default hello world but command line does_

I just wanted to make an Android app with blank activity with no fragment view.

like image 433
Christian Burgos Avatar asked Mar 09 '14 22:03

Christian Burgos


People also ask

Can you call an activity from a fragment?

Best way of calling Activity from Fragment class is that make interface in Fragment and add onItemClick() method in that interface. Now implement it to your first activity and call second activity from there.

Can a fragment be an activity?

We can call a fragment is a kind of sub-activity. It is always hosted by an activity. It has its own layout and its own behavior with its own life cycle callbacks. We can add or remove fragments in an activity while the activity is running.

How do I attach a fragment to an activity?

There are two ways to add a fragment to an activity: dynamically using Java and statically using XML. Before embedding a "support" fragment in an Activity make sure the Activity is changed to extend from FragmentActivity or AppCompatActivity which adds support for the fragment manager to all Android versions.

Can fragment extends activity?

FragmentActivity does not inherit from Fragment . Hence, you cannot inherit from Activity or FragmentActivity and somehow also inherit from Fragment .


2 Answers

For those who would like instructions on how to remove Fragments from the project:

1) Copy all the contents of res/layout/fragment_main.xml. Open activity_main.xml, delete the FrameLayout, and paste in the copied contents.

2) Delete fragment_main.xml

3) In MainActivity.java, delete the whole PlaceHolderFragment class:

/**  * 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;     } } 

4) Delete the following lines from onCreate():

if (savedInstanceState == null) {     getSupportFragmentManager().beginTransaction()             .add(R.id.container, new PlaceholderFragment()).commit(); } 

At this point you should be all set to run the project.

like image 161
Ben Jakuben Avatar answered Sep 20 '22 20:09

Ben Jakuben


You can use the previous template by editing the template files inside the Android SDK folder. But, make sure you have a backup of the "BlankActivity" folder.

  • Go to /templates/tools/templates/activities/BlankActivity

  • Locate the template.xml file and locate the following piece of code

     <parameter   id="fragmentLayoutName"   name="Fragment Layout Name"   type="string"   constraints="layout|unique|nonempty"   suggest="fragment_${classToResource(activityClass)}"   default="fragment_main"   help="The name of the layout to create for the activity's content fragment" /> 

and change the constraints to constraints="empty".

  • Locate recipie.xml.ftl and locate the following piece of code
    <instantiate from="res/layout/fragment_simple.xml.ftl" to="${escapeXmlAttribute(resOut)}/layout/${fragmentLayoutName}.xml" />
    and comment the whole line.

  • Locate SampleActivity.java.ftl file inside root/src/app_package and delete these two lines
    <#include "include_options_menu.java.ftl"> <#include "include_fragment.java.ftl">

  • Locate fragment_simple.xml.ftl and activity_fragment_container.xml.ftl file inside root/res/layout. Copy the contents of fragment_simple.xml.ftl file to activity_fragment_container.xml.ftl file.

Now when you try to create a new activity, you'll get this screen new activity without fragment

You can leave the Fragment Layout Name field blank.

This works fine for me in the case of blank activity. I'm not sure if this is the right approach, with discarding fragments and all, but this works for me.

like image 44
saran Avatar answered Sep 17 '22 20:09

saran