14

पर विफल रहता है मेरे पास एक ऐसा एप्लिकेशन है जो अधिसूचना जारी करता है जब चयनित गतिविधि शुरू होती है। एंड्रॉइड डॉक्स के अनुसार मैं यह जांचने के लिए Navutils.ShouldUpRecreateTask का उपयोग कर सकता हूं कि गतिविधि सीधे शुरू हो गई है (यानी अधिसूचना से) या सामान्य गतिविधि स्टैक के माध्यम से। हालांकि यह गलत जवाब देता है। मैं जेलीबीन पर इसका परीक्षण कर रहा हूं लेकिन समर्थन पुस्तकालय का उपयोग कर रहा हूं।NavUtils.shouldUpRecreateTask जेलीबीन

मूल रूप से चाहिए अपरिवर्तित कार्य हमेशा झूठा लौटाता है, भले ही गतिविधि शुरू हो गई हो।

किसी भी विचार पर क्यों अपर रिकिकेट टास्क सही जवाब देने में विफल रहा है?

+0

हाय क्लाइड, क्या जेलीबीन से पहले आपके लिए काम करना चाहिए? मुझे हमेशा एमुलेटर पर झूठा लगता है, भले ही मैंने अधिसूचना खोलने से पहले अपने ऐप को मार डाला हो। – Maragues

+0

मुझे वास्तव में एक ही समस्या है। AFAIK, ऐसा टूटा हुआ प्रतीत होता है। –

+0

देखें http://stackoverflow.com/questions/14602283/up-navigation-broken-on-jellybean – riwnodennyk

उत्तर

5

मुझे अभी भी पता नहीं है क्योंUpRecreateTask विफल रहता है - इसके लिए स्रोत कोड को देखने में बहुत मदद नहीं मिलती है। लेकिन समाधान काफी सरल है - मैं अधिसूचना से जुड़ी मंशा के लिए एक अतिरिक्त ध्वज मूल्य जोड़ता हूं, और इसे क्रिएट() में जांचता हूं। यदि यह सेट किया गया है, तो गतिविधि अधिसूचना से लागू की गई है, इसलिए बैक स्टैक को फिर से बनाया जाना है।

कोड इस तरह दिखता है:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Bundle b = getIntent().getExtras(); 
    fromNotification = b.getInt("fromNotification") == 1; 
    setContentView(R.layout.threadlist); 
} 

@Override 
public boolean onHomeButtonPressed() { 
    if(fromNotification) { 
     // This activity is not part of the application's task, so create a new task 
     // with a synthesized back stack. 
     TaskStackBuilder tsb = TaskStackBuilder.from(this) 
       .addNextIntent(new Intent(this, COPAme.class)); 
     tsb.startActivities(); 
    } 
     // Otherwise, This activity is part of the application's task, so simply 
     // navigate up to the hierarchical parent activity. 
    finish(); 
    return true; 
} 
+1

इससे मदद नहीं मिलेगी, अगर हम पहले से ही एप्लिकेशन का उपयोग कर रहे थे तो हम वास्तविक बैकस्टैक को नष्ट कर देंगे। यह ठीक है अगर हमारा ऐप बंद था। – Maragues

+0

@ मारग्स यदि गतिविधि पहले से ही खुली है, तो क्रिएट को कॉल नहीं किया जाता है - ऑन न्यूइन्टेंट को इसके बजाय बुलाया जाता है, इसलिए नोटिफिकेशन सेट नहीं है, इसलिए बैक स्टैक बरकरार रहता है। यदि गतिविधि खुली नहीं है, तो मौजूदा बैक स्टैक को नष्ट करना ठीक वही है जो मैं चाहता हूं। – Clyde

+0

getInt ("नॉटिफिकेशन") == 1 का उपयोग क्यों करें यदि आप getBoolean ("notification", false) जैसे कुछ का उपयोग कर सकते हैं? – ademar111190

4

यह सही नहीं है! http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse

इसलिए अधिसूचना बनाते समय आप इस करना होगा:

Intent resultIntent = new Intent(this, ResultActivity.class); 
// ResultActivity is the activity you'll land on, of course 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
// Adds the back stack 
stackBuilder.addParentStack(ResultActivity.class); 
// Adds the Intent to the top of the stack 
// make sure that in the manifest ResultActivity has parent specified!!! 
stackBuilder.addNextIntent(resultIntent); 
// Gets a PendingIntent containing the entire back stack 
PendingIntent resultPendingIntent = 
     stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

और फिर जब तुम जब आप अधिसूचना से शुरू आप ढेर जब अधिसूचना निर्माण यहां बताए बनाने के लिए यूपी बटन पर क्लिक करें जिसे आपको नियमित कोड की आवश्यकता है, जो है:

if (NavUtils.shouldUpRecreateTask(this, intent)) { 
    // This activity is NOT part of this app's task, so 
    // create a new task when navigating up, with a 
    // synthesized back stack. 
    TaskStackBuilder.create(this) 
    // Add all of this activity's parents to the back stack 
      .addNextIntentWithParentStack(intent) 
      // Navigate up to the closest parent 
      .startActivities(); 
} else { 
    NavUtils.navigateUpTo(this, intent); 
} 

यह मेरे लिए पूरी तरह से काम करता है।

+0

+1, यह मेरे लिए सबसे अच्छा/सबसे सही जवाब लगता है। धन्यवाद। – akent

+1

टास्कस्टैकबिल्डर .getPendingIntent ध्वज FLAG_ACTIVITY_CLEAR_TASK को इरादे पर सेट करेगा, जिसके कारण ऑन न्यूइन्टेंट() 2.3 से ऊपर की अपेक्षा नहीं की जाएगी। इसलिए, मेरी राय में, टास्कस्टैकबिल्डर को भूल जाएं, इसके बजाए सामान्य लंबित इन्टेंट का उपयोग करें। – jiashie

3

मुझे ओपी के समान समस्या थी। NavUtils.shouldUpRecreateTask हमेशा झूठी वापसी लग रहा था। (जेलीबीन भी) मैंने निम्नलिखित कार्यक्षमता को प्राप्त करने के लिए निम्नलिखित का उपयोग किया।

case android.R.id.home: 
Intent upIntent = new Intent(this,ParentActivity.class); 
upIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
startActivity(upIntent); 
finish(); 
return true; 

हार्ड कोडिंग के बजाय 'पैरेंट' इरादा इस तरह से लाया जा सकता है।

Intent upIntent = NavUtils.getParentActivityIntent(this); 
संबंधित मुद्दे