【问题标题】:Go back from activity to fragment with Up button使用向上按钮从活动返回到片段
【发布时间】:2016-11-14 13:44:06
【问题描述】:

在主要活动中,我创建了两个片段用作选项卡式菜单。其中一个片段包含一个列表,其中列表项在单击时启动一个新活动。我想使用向上操作/导航按钮从新活动返回到片段(列表),但没有显示列表,只有一个空选项卡。我已将父活动声明为 manifest.xml 中的主要活动。

当我使用手机的后退按钮时,它工作正常。

List 是在 Fragment 的 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);
                                            }
                                        }
        );

    } }

【问题讨论】:

  • 添加一些代码,从描述很难判断。您是否在清单中设置了父活动?

标签: android android-layout android-fragments android-activity


【解决方案1】:

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;
}

【讨论】:

  • 您也可以在finish(); 之前使用setResult(Activity.RESULT_CANCELED);,让您返回的Activity 能够响应以这种方式完成的“子”Activity。
【解决方案2】:

您必须如下定义导航按钮

@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);
}

【讨论】:

    【解决方案3】:

    试试这个,希望它对你有用!

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多