2015-09-16 17 views
5

के पीछे बटन पर क्लिक करने पर गतिविधि बंद नहीं है छवि, enter image description here
मैं पिछली गतिविधि पर वापस जाना चाहता हूं।
लेकिन टूलबार पर बैक बटन पर क्लिक करने पर, कुछ भी नहीं हो रहा है।
मैंने अभी भी कोई भाग्य प्राप्त करने के लिए निम्न कोड का उपयोग नहीं किया है।टूलबार

public boolean onOptionsItemSelected(MenuItem item){ 
     if(item.getItemId() == R.id.home) 
     { 
      finish(); 
     } 
     return true; 
    } 

उत्तर

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

अभी भी काम नहीं ... – ojas

+0

ठीक है, मेरे संपादन –

+1

आप getSupportActionBar() का उपयोग करने की जरूरत है इसे 'android.R.id.home' –

0

अपनी गतिविधि पर onBackPressed() विधि जोड़ें। और यह सुपर। और जब this.onBackPressed() पर वापस बटन कॉल पर क्लिक करें। इस के लिए अद्यतन कोड:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     this.onBackPressed(); 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

@Override 
public void onBackPressed() { 
    super.onBackPressed(); 
} 
+0

क्या आप इसके लिए कोड लिख सकते हैं .. – ojas

+0

मैंने –

+0

के लिए कोड उदाहरण अपडेट किया है 'getSupportActionBar() सेट करेंहोमबटन एनेबल (सत्य); \t \t getSupportActionBar()। SetDisplayHomeAsUpEnabled (true); ' –

2

अपने onOptionsItemSelected विधि

public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == android.R.id.home) { 
     finish(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

मैं काम करेंगे में अपने OnCreate विधि

toolbar.setTitle(R.string.title_activity_setting); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

में।

संबंधित मुद्दे