android Action bar 返回按钮无效

文章标签: android,android-actionbar
2015-1-15 9:09:53     人阅读    

android Action bar 返回按钮无效


如下图所示,我想在action bar添加返回按钮,返回到上一个视图,但是点击无效,下面是我的代码和配置文件


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>     

 

代码如下

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

 

处理方法

经过不断调试,解决方法如下:(新人解决什么东西都慢)

解决方法:
在  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);
}
}

在 MainActivity 中

把下面的代码:

public class HomeActivity extends BaseActivity


变成

public class HomeActivity extends FragmentActivity

在GalleryFragment 代码中
使用 Intent传递到 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());
 
    } 


原文地址:http://www.itmmd.com/201501/495.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

Cannot load library: reloc_library[1285]: cannot locate 'rand'   上一篇
下一篇  android开发app删除sim卡联系人
精彩回复
我要追加问题,请求站长解决!
姓名:       

《程序员app》专门为程序员量身定做!