2014-09-10 106 views
1

我正在研究一些应用程序,我希望实现与Gmail应用程序一样的访问应用程序主活动的相同模式。下面是从Android Developer site图片:通过从小部件开始的子活动导航到主活动

enter image description here

基本上我已经做了约点击之类的东西的一切,当我按下home键或后退按钮硬件问题。第一个(主页按钮)在onOptionsItemSelected中启动代码,但对于第二个(后退硬件按钮),我不知道如何覆盖它。以下是我尝试实现与Gmail应用程序相同模式的代码:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       Intent intent = new Intent(this, MainActivity.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 
       finish(); 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

对此有任何帮助将不胜感激。

编辑:由于mansoulx和杰克,可以用onBackPresed()方法覆盖硬件按钮动作。问题的一部分已经解决,但我仍然没有弄清楚如何使用Intents实现与Gmail应用程序相同的模式。编辑2:我实现了与Android Developer site相同的代码,但它仍然不起作用。我添加了对该代码的IF语句的否定,然后想要的模式开始工作,但是出现了错误的动画(从多任务菜单中更改应用程序时使用的动画)。我不认为这是对的,所以我仍在寻求帮助。这是我使用的代码(无否定):

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      Intent upIntent = NavUtils.getParentActivityIntent(this); 
      if (NavUtils.shouldUpRecreateTask(this, upIntent)) { 
       TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities(); 
      } else { 
       NavUtils.navigateUpTo(this, upIntent); 
      } 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

回答

0

您可以通过覆盖Activity.onBackPressed()来覆盖返回按钮。

您还可以查看此链接以实施后退导航。 http://developer.android.com/training/implementing-navigation/ancestral.html

答到EDIT2: 这是不建议的行为,但我个人做这样的事情:

parentActivity:

startActivity(new Intent(this, childactivity.class).putExtra("return", true)); 

childActivity.onBackPress:

if (getIntent().getBooleanExtra("return", false)) 
    super.onBackPress(); 
else { 
    finish(); 
    startActivity(new Intent(getApplicationContext(), parentactivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 
} 
+0

记得删除**如果你想使用onBackPressed()超级** – ymerdrengene 2014-09-10 13:25:33

+0

我实现了代码“与浏览了一个新的后退堆栈“并且它仍然不起作用。请看看我的编辑2. – user1257255 2014-09-10 15:43:52

+0

我编辑了我的答案 – konmik 2014-09-15 11:02:50

0
@Override 
public boolean onOptionsItemSelected(

    switch (menuItem.getItemId()) { 
    case android.R.id.home: 
     // code for Up button click 
     break; 

    return super.onOptionsItemSelected(item); 
} 


@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 
    // code for Back button 
} 

This link可能会帮助你。

+0

感谢部分解决方案,但从儿童活动返回的问题仍然存在。看编辑的内容。 – user1257255 2014-09-10 13:22:23

+0

我的答案中的链接解释了如何让一项活动成为另一项活动的家长。因此,对小孩调用“完成”将从堆栈中打开父项。 – 2014-09-10 14:51:49

+0

我做了所有必要的事情来实现它在该链接上的覆盖范围,但它仍然不起作用。如果我通过小部件开始活动,然后点击向上按钮,我会回到主屏幕和小部件。我想回到应用程序的主要活动,这是我通过小部件访问的活动的父项。 – user1257255 2014-09-10 14:58:22

0

我解决了这个问题,此代码在widget的IntentService:

Intent launchIntent = new Intent(this, ChildActivity.class); 
PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(getBaseContext()) 
     .addNextIntentWithParentStack(launchIntent) 
     .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

view.setOnClickPendingIntent(R.id.widget, clickPendingIntentTemplate);