2016-11-14 92 views
-1

在主要活動中,我創建了兩個片段用作選項卡式菜單。其中一個片段包含一個列表,其中列表項目單擊時會開始一個新活動。我想使用Up Action/Navigation按鈕從新活動返回到片段(列表),但沒有列表顯示,只有一個空標籤。我已經聲明父活動是manifest.xml中的主要活動。回到活動片段與上按鈕

當我使用手機的後退按鈕它工作正常。

列表在片段的onViewCreated方法中創建。

清單:

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".Main2Activity" 
     android:parentActivityName=".MainActivity" > 
    </activity> 
</application> 

片段與ListView控件其啓動新的活動:

public static class frag extends Fragment { 

    private static final String ARG_SECTION_NUMBER = "section_number"; 

    public frag() { 
    } 
    public static frag newInstance(int sectionNumber) { 
     frag fragment = new rangingFrag(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 


     return rootView; 
    } 

    @Override 
    public void onViewCreated(View rootView, Bundle savedInstanceState) { 
     ListView listView = (ListView) rootView.findViewById(R.id.mobile_list); 
     final ArrayAdapter adapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.listview); 
     listView.setAdapter(adapter); 
     adapter.addAll(beacons); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
              public void onItemClick(AdapterView<?> parent, View v, 
                    int position, long id) { 
               String selectedText = (String) parent.getItemAtPosition(position); 
               Bundle bundle = new Bundle(); 
               bundle.putString("param1", selectedText); 
               Intent myIntent = new Intent(v.getContext(), Main2Activity.class); 
               myIntent.putExtras(bundle); 
               startActivity(myIntent); 
              } 
             } 
     ); 

    } } 
+0

添加一些代碼,很難從描述中判斷。您是否在清單中設置了父級活動? – Rafal

回答

4

在你Activity要保持背部箭頭操作欄,在onCreate中,寫入:

ActionBar actionBar = getSupportActionBar(); 
actionBar.setDisplayHomeAsUpEnabled(true); 

,然後覆蓋:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    super.onOptionsItemSelected(item); 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      finish(); 
      break; 
    } 

    return true; 
} 
+0

您也可以在'finish();'之前使用'setResult(Activity.RESULT_CANCELED);'給出您正在返回的活動,以便以這種方式完成對「子」活動的響應能力。 –

0

您必須定義您的導航按鈕,如下

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // handle button click here 
    if (item.getItemId() == android.R.id.home) { 
     setResult(Activity.RESULT_CANCELED); 
     finish(); // close this activity and return to preview activity 
    } 
    return super.onOptionsItemSelected(item); 
} 
0

試試這個,希望它會爲你工作!

public boolean onOptionsItemSelected(MenuItem item) { 
    if (item.getItemId() == android.R.id.home) { 
     Intent intentforBackButton = NavUtils.getParentActivityIntent(this); 
     intentforBackButton.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     NavUtils.navigateUpTo(this, intentforBackButton); 
     return true; 
     } 
    return super.onOptionsItemSelected(item); 
}