0

using android 2.3.3 and Android API 25.

having 3 activities activity_main , activity_details and activity_settings. I've used android studio auto generating for creating these activities as blank activity with fragment, So manifest code is generated automatically.

activity_main and activity_details have menu option Settings that's for opening the activity_settings.

clicking on Settings option menu for activity_details works and opens activity_settings but not working for activity_main

and here is my code:

MainActivityFragment.java and DetailActivityFragment.java (same)

`@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_settings) {
        startActivity(new Intent(getActivity(), SettingsActivity.class));
        return true;
    }
}`

menu_detail.xml

`<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.poula.sunshine.DetailActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="Settings"
        app:showAsAction="never" />
</menu>`

menu_main.xml

`<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.poula.sunshine.MainActivity" >
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="Settings"
        app:showAsAction="never" />
</menu>`
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Poula Adel
  • 609
  • 1
  • 10
  • 33
  • 3
    post code (never post images of code) , we need to see more of it – Pavneet_Singh Jun 12 '17 at 17:13
  • which part exactly ? I just don't want readers to get bored. – Poula Adel Jun 12 '17 at 17:15
  • you need to post complete `activity_main.java` file , post all related java and xml files code – Pavneet_Singh Jun 12 '17 at 17:29
  • what happens on clicking it? do you get anything on LogCat? – Adithya Jun 12 '17 at 17:33
  • at API 25 there is 2 files created when using **blank activity with fragment** `MyfileActivity` and `MyfileActivityFragment` .. which one? – Poula Adel Jun 12 '17 at 17:37
  • I knew the problem !! I'm posting the solution in a minute – Poula Adel Jun 12 '17 at 17:39
  • post `MyfileActivity` – Pavneet_Singh Jun 12 '17 at 17:45
  • when creating new `blank activity with fragment` **Android Studio** by default automatically generate `FilenameActivity.java`, `FilenameActivityFragment.java` and also other files for design.. the hierarchy that code run with when I try to override `onOptionsItemSelected` method in `FilenameActivityFragment.java` it runs the default code generated in `onOptionsItemSelected` method in `FilenameActivity.java` (just return true),, but if someone can type a more readable answer and describe it better it will be great if you got me @Pavneet_Singh – Poula Adel Jun 12 '17 at 17:59
  • 1
    https://stackoverflow.com/questions/8308695/android-options-menu-in-fragment – Pavneet_Singh Jun 12 '17 at 18:09

0 Answers0