user6594036
user6594036

Reputation:

how to use getSupportFragmentManager() in Activity

When I change

public class Bmr extends Activity implements View.OnClickListener {

to

public class Bmr extends appcompatactivity implements View.OnClickListener {

this getSupportFragmentManager() is working but I need to work with Activity class. Are there any options how to do it?

Upvotes: 1

Views: 15485

Answers (2)

Umar Ata
Umar Ata

Reputation: 4258

First of all as stated above you need to use AppcompatActivity

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            FragmentManager fm=getSupportFragmentManager();
            FragmentTransaction ft= fm.beginTransaction();
            ft.replace(R.id.mainframe,tab1Fragment);
            ft.commit();
         }
}

here is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:padding="10dp">
 <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mainframe"></FrameLayout>

</LinearLayout>

Upvotes: 0

Eric B.
Eric B.

Reputation: 4702

You can't. getSupportFragmentManager() is only available in FragmentActivity and classes that extend it.

From the docs, you can see that AppCompatActivity is a Activity, so everything in Activity is also available in AppCompatActivity.

java.lang.Object
   ↳    android.content.Context
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity
                   ↳    android.support.v4.app.FragmentActivity
                       ↳    android.support.v7.app.AppCompatActivity

Upvotes: 6

Related Questions