How to move to a fragment from an activity with android studio

1

I am developing my application and I arrived at a point where I need to open an activity that has tabs from another activity through a button. It would be something like this:

I tried it through intent but I did not get anything, and I have not managed to find examples that can serve as a guide for me to do it.

If anyone can guide me or tell me how to do it, I would really appreciate it.

Attached MainActivity code that has the button:

public class MainActivity extends AppCompatActivity {
Button entrar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    entrar = (Button) findViewById(R.id.button);
    entrar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent startIntent = new Intent(MainActivity.this, Inicio.class);
            startActivity(startIntent);
        }
    });
}
}

My Activity with Tabs that I named start:

public class Inicio extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inicio);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);


}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                Tab1 tab1 = new Tab1();
                return tab1;
            case 1:
                Tab2 tab2 = new Tab2();
                return tab2;
            case 2:
                Tab3 tab3 = new Tab3();
                return tab3;

            default:
                return null;
        }

    }


    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "TAB 1";
            case 1:
                return "TAB 2";
            case 2:
                return "TAB 3";

        }
        return null;
    }
}
}

Now the Fragments (each in a separate Activity, called Tab1.java etc ...):

public class Tab1 extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab_1, container, false);
        return rootView;
    }


}

public class Tab2 extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab_2, container, false);
        return rootView;
    }


}

public class Tab3 extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab_3, container, false);
        return rootView;
    }


}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.tabsfragment">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Inicio"></activity>
</application>

</manifest>
    
asked by flo 19.07.2017 в 23:26
source

2 answers

0

If what you want is only to launch another activity from which you find yourself, it would be enough:

//  Clase MainActivity
public void llamarActivity(){
    Intent intent = new Intent(MainActivity.this, TabsActivity.class);
    startActivity(intent);
}

Being, MainActivity where you want to call it from and TabsActivity the name of the class linked to the activity of the tabs.

And this method must be called from the button in the XML of your activity_main:

//  activity_main.xml
<Button
    android:id="@+id/boton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp"
    android:onClick="llamarActivity"/>

With this you solve the call to the other activity. Greetings.

    
answered by 20.07.2017 в 00:49
0
07-20 18:05:17.371 3393-3393/? I/art: Not late-enabling -Xcheck:jni (already on)
07-20 18:05:17.371 3393-3393/? W/art: Unexpected CPU variant for X86 using defaults: x86
07-20 18:05:17.564 3393-3393/com.example.user.tabsfragment W/System: ClassLoader referenced unknown path: /data/app/com.example.user.tabsfragment-2/lib/x86
07-20 18:05:17.585 3393-3393/com.example.user.tabsfragment I/InstantRun: starting instant run server: is main process
07-20 18:05:17.720 3393-3393/com.example.user.tabsfragment W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-20 18:05:17.834 3393-3421/com.example.user.tabsfragment I/OpenGLRenderer: Initialized EGL, version 1.4
07-20 18:05:17.834 3393-3421/com.example.user.tabsfragment D/OpenGLRenderer: Swap behavior 1
07-20 18:05:17.834 3393-3421/com.example.user.tabsfragment W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
07-20 18:05:17.834 3393-3421/com.example.user.tabsfragment D/OpenGLRenderer: Swap behavior 0
07-20 18:05:28.852 3393-3393/com.example.user.tabsfragment D/AndroidRuntime: Shutting down VM
07-20 18:05:28.854 3393-3393/com.example.user.tabsfragment E/AndroidRuntime: 
FATAL EXCEPTION: main

Process: com.example.user.tabsfragment, PID: 3393

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.tabsfragment/com.example.user.tabsfragment.Inicio}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)

at android.app.ActivityThread.-wrap12(ActivityThread.java)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6119)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:207)

at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)

at com.example.user.tabsfragment.Inicio.onCreate(Inicio.java:26)

at android.app.Activity.performCreate(Activity.java:6679)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 

at android.app.ActivityThread.-wrap12(ActivityThread.java) 

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 

at android.os.Handler.dispatchMessage(Handler.java:102) 

at android.os.Looper.loop(Looper.java:154) 

at android.app.ActivityThread.main(ActivityThread.java:6119) 

at java.lang.reflect.Method.invoke(Native Method) 

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Hi, I managed to fix the error by removing the Toolbar from the TabsActivity because when I saw my log it said there was a problem with my action bar and the toolbar, I deleted it and the problem was solved.

Thank you very much for your help!

    
answered by 20.07.2017 в 20:22