誰かが私の問題を解決してくれることを願っています。Android の [戻る] ボタンは、一部のアクティビティで機能し、他のアクティビティでは機能しません。私の MainActivity には、選択したオプションに応じて、他のターゲット アクティビティがあります。私のアプリはマップを管理しているので、マーカーの情報ウィンドウをクリックすると、必要に応じて詳細アクティビティに移動し、Android の [戻る] ボタンをクリックすると、通常どおりメイン アクティビティに戻ります (実装したくないアクションバーのホームボタン)。
しかし、「連絡先」または「利用規約」オプションをタップし、情報ウィンドウと同じ方法で連絡先/利用規約アクティビティを開始しようとすると、通常どおりアクティビティが開きますが、戻るボタンは開きません。うまくいきません。Contact/Terms アクティビティを再開し、メイン アクティビティには戻らないようです。多くの投稿を検索し、観察結果を追加します。
- MainActivity で finish() を呼び出さないので、意図的にアクティビティ スタックをいじっていません。
- この投稿が示唆するように、私は putExtra("finishActivityOnSaveCompleted", true) を試しましたが、うまくいきませんでした。
- os.bundle、app.Activity、content.Intent、view.View、View.Window 以外のインポートがないシンプルなレイアウトを表示するだけです。
- 一部の関数をオーバーライドするメソッドから Contact Activity を呼び出す startActivity() 関数を起動しようとしました (詳細なアクティビティは、クリック イベントをオーバーライドするメソッドから startActivity で呼び出されます)。
- メインから連絡先アクティビティに追加のデータを入れたり受け取ったりしようとしました(それは私がどれほど絶望的かです)。
オプションが不足していて、何が問題なのかわかりません。Android が処理してくれるので、戻るボタン機能を自分で実装するべきではないことはわかっています。マニフェストで SDK バージョン 16 以降をターゲットにしています。両方のアクティビティを呼び出す短いバージョンのコードを追加します。
public class MainActivity extends FragmentActivity implements OnNavigationListener, ClusterManager.OnClusterClickListener<MyItem>, ClusterManager.OnClusterInfoWindowClickListener<MyItem>, ClusterManager.OnClusterItemClickListener<MyItem>, ClusterManager.OnClusterItemInfoWindowClickListener<MyItem>
{
.
.
.
/*Some variables initialized*/
.
.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
.
.
.
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
menuOpt=position;
getData();
//gridView.setVisibility(View.GONE);
if(gridView.getVisibility() == View.VISIBLE) {
toggleView(gridView);
}
}
});
}
public void getData(){
String optString="";
llMatVesp.setVisibility(View.GONE);
switch(menuOpt){
/* In this case the back button to MainActivity works, I manage action bar operations for a particular behaviour of the menu, all options in the switch do */
case 1:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setCustomView(null);
getResults();
break;
/* In this case the back button of Contact Activity, that's supposed to return to MainActivity doesn't work */
case 2:
Intent conIntent=new Intent(MainActivity.this, Contact.class); startActivity(conIntent);
break;
default:break;
}
}
/*this function brings data from db, add markers to the map, and animates to a certain point
public void getResults(){
.
.
}
/* This functions extract the right data from the marker and make the intent that starts the detail Activity, where the back button works with no problems */
@Override
public void onClusterItemInfoWindowClick(MyItem item) {
showDetail(item.getId(),item.getMenuOpt());
}
public void showDetail(String id, int menuOpt){
Intent showDetail=new Intent(MainActivity.this, ShowDetail.class);
showDetail.putExtra("menuOpt", menuOpt);
showDetail.putExtra("icon", ic);
showDetail.putExtra("id", id);
startActivity(showDetail);
}
}
Contact アクティビティにはフォームがあり、メールを送信しますが、戻るボタンが機能しないもう 1 つのアクティビティである TermsAndConditions アクティビティは、単純な LinearLayout のみを表示するだけで、凝ったコーディングはありません。