在主要活动中,我创建了两个用作标签菜单的片段。其中一个片段包含一个列表,其中列表项在单击时开始新活动。我想使用Up Action / Navigation按钮从新活动返回到片段(列表),但不显示任何列表,只显示空标签。我已将parent活动声明为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 :(得分:5)
在<table class="locale-wrapper">
<tr>
<td class="locale-text">SL</td>
<td class="locale-text">EN</td>
</tr>
</table>
中,您希望在操作栏上保留后退箭头,在Activity
中,写一下:
onCreate
然后覆盖:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
答案 1 :(得分: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);
}
答案 2 :(得分: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);
}