Open a new Activity from within a Fragment

Asked

Viewed 1,428 times

4

How do I exit the Fragment screen (extends Fragment) and go to a normal activity (extends AppCompatActivity)? Either way, even using a button, I can’t. It never works.

public class Teste extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teste);
    }
}

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}



@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    FragmentManager fragmentManager = getFragmentManager();

    //noinspection SimplifiableIfStatement
    if (id == R.id.botao_tela_status) {

        fragmentManager.beginTransaction().replace(R.id.conteudo_fragmento, new FragmentoStatusTela()).commit();

        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    FragmentManager fragmentManager = getFragmentManager();

    if (id == R.id.nav_quiz_tela) {

        fragmentManager.beginTransaction().replace(R.id.conteudo_fragmento, new FragmentoQuiz()).commit();

    } else if (id == R.id.desafio2) {

        Intent intent = new Intent(MainActivity.this, Teste.class);
        startActivity(intent);

    } else if (id == R.id.desafio3) {

    } else if (id == R.id.desafio4) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

12-18 17:32:17.776 3532-3532/com.example.alexandre.nossa_historia W/PathParser: Points are too far apart 4.000000596046461
12-18 17:32:17.790 3532-3532/com.example.alexandre.nossa_historia D/AndroidRuntime: Shutting down VM
12-18 17:32:17.791 3532-3532/com.example.alexandre.nossa_historia E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.alexandre.nossa_historia, PID: 3532
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alexandre.nossa_historia/com.example.alexandre.nossa_historia.Main2Activity}: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                                     Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }
                                                                                        at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:471)
                                                                                        at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:325)
                                                                                        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:286)
                                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                        at com.example.alexandre.nossa_historia.Main2Activity.onCreate(Main2Activity.java:11)
                                                                                        at android.app.Activity.performCreate(Activity.java:5990)
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:135) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
12-18 17:32:20.471 3532-3532/com.example.alexandre.nossa_historia I/Process: Sending signal. PID: 3532 SIG: 9

  • Possible duplicate of Error opening new Activity

  • And How can I solve ? Please help me

  • Read the accepted answer to the other question. It’s that there.

  • I did it to keep up the mistake

  • remember that I am using a class with the extension Fragment

  • It’s similar, but I’ll put a specific answer to Ragments here then. Just a minute.

Show 1 more comment

2 answers

2


Analyzing Stacktrace, the problem is in the application theme. You should add the following lines in the theme for this reaction to work:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

Old version of the answer:

Analyzing the previous comments the problem seems to be a Nullexception in the button trigger.

Sorry Pablo, but still not sure because if I go to xml and put onClick="click_here" and run the application already closes

As you are calling the event through XML, it must be resulting in a Nullexception of the Button because it is not being inflated correctly. Since you didn’t post any code snippets, I can’t say for sure.

But anyway, try to change some things in your code, for example:

In XML: Remove the "onClick" from the button and add an ID to it.

In the Fragment, do:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment, container, false);

    Button btn = view.findViewById(R.id.id_btn);

     /*
     * Define a ação do Botão, é o mesmo que você está fazendo através do 
     * onClick no XML.
     */
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentActivity act = getActivity();

            if (act != null) {
                startActivity(new Intent(act, OutraActivity.class));
            }
        }
    });

    return view;
}

Please note that the excerpt "R.layout.Fragment" should be the name of your layout and "R.id.id_btn" must be the ID you gave the button.

  • Look at my code I just put in and he’s so wrong I don’t know why

  • Could post the error that is happening, please. And if possible, the Fragment code as well.

  • I just made the mistake

  • Okay, seeing Stacktrace, we can realize that the problem is another.. Try to put in the theme of your app <item name="windowActionBar">false</item> &#xA; <item name="windowNoTitle">true</item>

  • Thanks a lot Amelco, it worked, that’s all, you’re great

  • I’m glad it worked out. I changed my answer with this solution.

Show 1 more comment

1

Inside the event that will open the new screen (button, for example), you will put something like this:

Intent abrirOutraActivity = new Intent(getActivity(), OutraActivity.class);
startActivity(abrirOutraActivity);

The difference here regarding opening from within an Activity is that you will use getActivity() in place of this or MinhaActivity.this, because this first parameter of the Intent has to be a Context and the Fragment is not a Context (but the Activity within which he is)

  • Sorry Pablo, but still not sure because if I go to xml and put onClick="click_here" and run the application already closes

  • I think I’m gonna give up

  • View You create a Navigationdrawer using the android studio template and try to call an Activity from the menu to see what happens, please help me

Browser other questions tagged

You are not signed in. Login or sign up in order to post.