本文介绍了操作栏后退按钮不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些的Andr​​oid文档的帮助。我是试图做一个操作栏返回button.I得到一个操作栏后退按钮这样的如下图:

输出:

不过,我的问题是看图库图片后,我preSS的操作栏后退按钮

然后它不工作。但它必须返回previous页

清单:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.fth.android
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =11
        机器人:targetSdkVersion =19/>

    <应用
        机器人:名称=com.sit.fth.app.GemsApplication
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:largeHeap =真
         机器人:主题=@安卓风格/ Theme.Holo.Light>

        <活动
            机器人:名称=com.sit.fth.activity.SplashActivity
            机器人:标签=@字符串/ APP_NAME
            机器人:screenOrientation =画像
            机器人:主题=@安卓风格/ Theme.Black.NoTitleBar.Fullscreen>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;

        <活动
            机器人:名称=com.sit.fth.activity.HomeActivity>

        < /活性GT;

        <活动机器人:名称=com.sit.fth.activity.YoutubePlayActivity>
        < /活性GT;

         <活动机器人:名称=com.sit.fth.activity.GalleryActivity>

        < /活性GT;

    < /用途>

< /舱单>
 

下面列出是的编码。

GalleryActivity.java:

 包com.sit.fth.activity;

    进口android.app.ActionBar;
    进口android.os.Bundle;
    进口android.support.v4.app.FragmentActivity;
    进口android.support.v4.app.NavUtils;
    进口android.view.MenuItem;

    进口com.fth.android.R;

   公共类GalleryActivity扩展FragmentActivity {

    私人诠释的位置;
    私有静态字符串ID;
    私有静态字符串名称;
    私人DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
    私人ViewPager mViewPager;


    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
         的setContentView(R.layout.activity_gallery);

            。位置= getIntent()getExtras()调用getInt(位置);

            。ID = getIntent()getExtras()的getString(ID);

            。NAME = getIntent()getExtras()的getString(姓名);

            mDemoCollectionPagerAdapter =新DemoCollectionPagerAdapter(getSupportFragmentManager());

            //设置操作栏。
            最后的动作条动作条= getActionBar();


            actionBar.setDisplayHomeAsUpEnabled(真正的);

           // getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_USE_LOGO|ActionBar.DISPLAY_HOME_AS_UP);

            //设置的ViewPager,将适配器。
            mViewPager =(ViewPager)findViewById(R.id.pager);
            mViewPager.setAdapter(mDemoCollectionPagerAdapter);
        }

        @覆盖
        公共布尔onOptionsItemSelected(菜单项项){
            开关(item.getItemId()){
                案例android.R.id.home:

                    意图upIntent =新的意图(这一点,HomeActivity.class);
                    upIntent.putExtra(位置,位置);
                    如果(NavUtils.shouldU precreateTask(这一点,upIntent)){

                        TaskStackBuilder.from(本)
                                .addNextIntent(upIntent)
                                .startActivities();
                        完();
                    } 其他 {

                        NavUtils.navigateUpTo(这一点,upIntent);
                    }
                    返回true;
            }
            返回super.onOptionsItemSelected(项目);
        }


      }
 

GalleryDetailFragment.java:

 包com.sit.fth.frgment;

进口com.sit.fth.model.GalleryDetail;
进口com.sit.fth.util.APIServiceHandler;
进口com.sit.fth.util.AppConstants;
进口com.sit.fth.util.AppPromoPager;

公共类GalleryDetailFragment扩展BaseFragment工具
        PromoPagerListener {

    私人AppPromoPager GridView控件;
    私人的LinearLayout loadingLayout;
    私人TextView的无形;
    私人字符串ID;
    私人字符串名称;
    名单<字符串>画廊;
    私人RelativeLayout的pagerLayout;
    私人TextView的countView;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        this.setHasOptionsMenu(真正的);
        。ID = getArguments()的getString(ID);
        。NAME = getArguments()的getString(姓名);
        查看查看= inflater.inflate(R.layout.app_pager,NULL);

        TextView中的TextView =((TextView中)view.findViewById(R.id.gal_header));
        textView.setTypeface(Typeface.DEFAULT);
        textView.setText(名称);

        GridView控件=(AppPromoPager)view.findViewById(R.id.pager);

        loadingLayout =(的LinearLayout)view.findViewById(R.id.ll_loading);
        隐形=(TextView中)view.findViewById(R.id.txt_invisible);
        pagerLayout =(RelativeLayout的)view.findViewById(R.id.ll_pager);
        countView =(TextView中)view.findViewById(R.id.txt_count);

        updateViews(View.VISIBLE,View.GONE,View.GONE);

        新GetGalTask​​(getActivity())执行()。

        返回查看;
    }

}
 

任何人都可以帮助我,如果你知道如何解决these.Thank你。

解决方案

我通过添加以下代码的 GalleryActivity 解决了这些问题。

 动作条动作条;
动作条= getActionBar();

actionBar.setDisplayHomeAsUpEnabled(真正的);

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例android.R.id.home:
            onBack pressed();
            返回true;
        }

    返回super.onOptionsItemSelected(项目);
}
 

MainActivity

previously,

然后我变成

GalleryFragment:

我用意图将其传递给 GalleryActivity

  @覆盖
    公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
        画廊画廊=(图库论坛)arg0.getAdapter()的getItem(ARG2)。

        意向意图=新的意图(getActivity(),GalleryActivity.class);
        intent.putExtra(位置,位置);
        intent.putExtra(ID,gallery.getGalId());
        intent.putExtra(姓名,gallery.getAlbumTitle());
        startActivity(意向);

        // mCallback.OnGalItemSelected(gallery.getGalId(),gallery.getAlbumTitle());
    }
 

with the help of these Android Docs.I am trying to do a action bar Back button.I get an Action Bar Back Button like these below image:

Output:

But My problem is After watching the Gallery images I press the action bar back button.

Then it is not working.But it have to go back to previous page.

Manifest:

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fth.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:name="com.sit.fth.app.GemsApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
         android:theme="@android:style/Theme.Holo.Light" >

        <activity
            android:name="com.sit.fth.activity.SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.sit.fth.activity.HomeActivity">

        </activity>

        <activity android:name="com.sit.fth.activity.YoutubePlayActivity" >
        </activity>

         <activity android:name="com.sit.fth.activity.GalleryActivity">

        </activity>

    </application>

</manifest>

Listed below are the codings.

GalleryActivity.java:

    package com.sit.fth.activity;

    import android.app.ActionBar;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.NavUtils;
    import android.view.MenuItem;

    import com.fth.android.R;

   public class GalleryActivity extends FragmentActivity {

    private int position;
    private static String id;
    private static String name;
    private DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
    private ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_gallery);

            position = getIntent().getExtras().getInt("position");

            id = getIntent().getExtras().getString("id");

            name = getIntent().getExtras().getString("name");

            mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());

            // Set up action bar.
            final ActionBar actionBar = getActionBar();


            actionBar.setDisplayHomeAsUpEnabled(true);

           // getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_USE_LOGO|ActionBar.DISPLAY_HOME_AS_UP);

            // Set up the ViewPager, attaching the adapter.
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mDemoCollectionPagerAdapter);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:

                    Intent upIntent = new Intent(this, HomeActivity.class);
                    upIntent.putExtra("position", position);
                    if (NavUtils.shouldUpRecreateTask(this, upIntent)) {

                        TaskStackBuilder.from(this)
                                .addNextIntent(upIntent)
                                .startActivities();
                        finish();
                    } else {

                        NavUtils.navigateUpTo(this, upIntent);
                    }
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }


      }

GalleryDetailFragment.java:

package com.sit.fth.frgment;

import com.sit.fth.model.GalleryDetail;
import com.sit.fth.util.APIServiceHandler;
import com.sit.fth.util.AppConstants;
import com.sit.fth.util.AppPromoPager;

public class GalleryDetailFragment extends BaseFragment implements
        PromoPagerListener {

    private AppPromoPager gridView;
    private LinearLayout loadingLayout;
    private TextView invisible;
    private String id;
    private String name;
    List<String> galleries;
    private RelativeLayout pagerLayout;
    private TextView countView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        this.setHasOptionsMenu(true);
        id = getArguments().getString("id");
        name = getArguments().getString("name");
        View view = inflater.inflate(R.layout.app_pager, null);

        TextView textView = ((TextView) view.findViewById(R.id.gal_header));
        textView.setTypeface(Typeface.DEFAULT);
        textView.setText(name);

        gridView = (AppPromoPager) view.findViewById(R.id.pager);

        loadingLayout = (LinearLayout) view.findViewById(R.id.ll_loading);
        invisible = (TextView) view.findViewById(R.id.txt_invisible);
        pagerLayout = (RelativeLayout) view.findViewById(R.id.ll_pager);
        countView = (TextView) view.findViewById(R.id.txt_count);

        updateViews(View.VISIBLE, View.GONE, View.GONE);

        new GetGalTask(getActivity()).execute();

        return view;
    }

}

Anybody can help me if you know how to solve these.Thank You.

解决方案

I solved these problem by adding the below coding in GalleryActivity.

ActionBar actionBar;
actionBar=getActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);

@Override
public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        }

    return super.onOptionsItemSelected(item);
}

In MainActivity:

Previously,

Then I change into

In GalleryFragment:

I use Intent to pass it to the GalleryActivity.

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Gallery gallery = (Gallery) arg0.getAdapter().getItem(arg2);

        Intent intent = new Intent(getActivity(), GalleryActivity.class);
        intent.putExtra("position", position);
        intent.putExtra("id", gallery.getGalId());
        intent.putExtra("name", gallery.getAlbumTitle());
        startActivity(intent);

        // mCallback.OnGalItemSelected(gallery.getGalId(),gallery.getAlbumTitle());
    }

这篇关于操作栏后退按钮不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 21:20