user3272367
user3272367

Reputation: 69

home button ends app in actionbar

I need some help, in my app, I have the first activity (main) then I call a new activity (with fragment, tabs, swipes) Now I want to enable the home button in the top left corner. and it works, but it end my app, and go back to my "phone screen" ?

here is my manifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="overskov.rhkbrand"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
         android:name="overskov.rhkbrand.MainActivity"                       
        android:label="@string/app_name"        
        >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="overskov.rhkbrand.Under_menu"
        android:label="@string/title_activity_under_menu" 
         android:parentActivityName="overskov.rhkbrand.MainActivity"
         >
        <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="overskov.rhkbrand.MainActivity" />

    </activity>
    <activity
        android:name="overskov.rhkbrand.Etage_menu"
        android:label="@string/title_activity_etage_menu" >

    </activity>
   </application>

</manifest>

and here my fragment

public class Under_menu extends FragmentActivity implements ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Opgang A", "Studie/Gang", "Opgang B", "Bar/Fest", "Opgang C", "Opgang D" };

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

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setDisplayHomeAsUpEnabled(true);

acording to android developers :

Once the parent activity is specified in the manifest like this and you enable the Up button with setDisplayHomeAsUpEnabled(), your work is done and the action bar properly navigates up.

So what am I doing wrong?

Thanks Dennis

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   




    Button button1 = (Button) findViewById(R.id.save);
    button1.setOnClickListener(new OnClickListener() {          
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), Under_menu.class));
            finish();           
        }
    });

    Button button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(new OnClickListener() {          
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), Etage_menu.class));
            finish();
        }


    });

}

}

Upvotes: 0

Views: 153

Answers (1)

Thein
Thein

Reputation: 3968

How about override the home button ?

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Upvotes: 1

Related Questions