Android 导致多个活动的共享首选项

Android 导致多个活动的共享首选项,android,android-fragments,android-activity,sharedpreferences,Android,Android Fragments,Android Activity,Sharedpreferences,我在onCreate中使用SharedReferences来检查用户是否已使用SharedReferences“登录”。我也试过在onStart里面做。但问题是,当用户看到“ProductCategories”后,当我单击移动屏幕的后退按钮时,我可以看到多个ProductCategories活动已经打开。我不断点击后退按钮几次,然后我得到第一个活动“MainActivity” java不包含任何内容,因为它必须显示一个名为HomePage.class的片段,我在ProductCategories

我在onCreate中使用SharedReferences来检查用户是否已使用SharedReferences“登录”。我也试过在onStart里面做。但问题是,当用户看到“ProductCategories”后,当我单击移动屏幕的后退按钮时,我可以看到多个ProductCategories活动已经打开。我不断点击后退按钮几次,然后我得到第一个活动“MainActivity”

java不包含任何内容,因为它必须显示一个名为HomePage.class的片段,我在ProductCategories的xml文件中提到过这个片段。我现在发布主页的代码:

包com.example.pager

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class HomePage extends Fragment {
    public HomePage(){}
    String[] listitems;
    int[] images = {R.drawable.cadburysilk,R.drawable.cadburys_dairymilk,R.drawable.perk,
            R.drawable.kitkat,R.drawable.nestlemunchchocolate,R.drawable.cadbury_bournville_bar,
            R.drawable.snickers};
    ListView list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreateView(inflater, container, savedInstanceState);

        View rootview = inflater.inflate(R.layout.activity_home_page, container, false);
        Resources res = getResources();
        listitems=res.getStringArray(R.array.items);
        list = (ListView)rootview.findViewById(R.id.itemslist);
        AdapterClass adapterClass = new AdapterClass(HomePage.this.getActivity(), listitems, images);
        list.setAdapter(adapterClass);
        return rootview;
    }
}

class AdapterClass extends ArrayAdapter<String>
{
    Context context;
    int[] images;
    String[] names;
    public AdapterClass(Context c, String[] items,int imgs[] ) {
        // TODO Auto-generated constructor stub
        super(c, R.layout.rowlayout, R.id.quantity, items );
        this.context = c;
        this.images = imgs;
        this.names = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row  = inflater.inflate(R.layout.rowlayout, parent, false);
        ImageView imageView = (ImageView) row.findViewById(R.id.img);
        TextView textView = (TextView) row.findViewById(R.id.textView1);


        imageView.setImageResource(images[position]);
        textView.setText(names[position]);
        return row;
    }
}
导入android.content.Context;
导入android.content.res.Resources;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.ListView;
导入android.widget.TextView;
公共类主页扩展片段{
公共主页(){}
字符串[]列表项;
int[]images={R.drawable.cadburysilk,R.drawable.cadburys_dairymilk,R.drawable.perk,
R.drawable.kitkat,R.drawable.雀巢巧克力,R.drawable.cadbury\u bournville\u bar,
R.drawable.snickers};
列表视图列表;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreateView(充气机、容器、保存状态);
视图根视图=充气机。充气(R.layout.activity\u主页,容器,false);
Resources res=getResources();
listitems=res.getStringArray(R.array.items);
list=(ListView)rootview.findViewById(R.id.itemslist);
AdapterClass AdapterClass=新AdapterClass(HomePage.this.getActivity()、列表项、图像);
列表.setAdapter(适配器类);
返回rootview;
}
}
类适配器类扩展ArrayAdapter
{
语境;
int[]图像;
字符串[]名称;
公共适配器类(上下文c,字符串[]项,int-imgs[]){
//TODO自动生成的构造函数存根
超级(c、R.layout.rowlayout、R.id.quantity、items);
this.context=c;
该图像=imgs;
this.names=项目;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图行=充气机。充气(R.layout.rowlayout,父级,false);
ImageView ImageView=(ImageView)row.findViewById(R.id.img);
TextView TextView=(TextView)row.findViewById(R.id.textView1);
setImageResource(图像[位置]);
setText(名称[位置]);
返回行;
}
}

您可以将清单中的ProductCategories定义任务更改为singleTask:

 <activity
        android:name=".ProductCategories"
        android:launchMode="singleTask"/>


show
ProductCategories
code@AntonKovalyov这是代码^。请看。为什么不显示
ProductCategories
activity代码?它什么都没做。MainActivity是如何启动的?是否定义为android.intent.category.LAUNCHER?将日志添加到MainActivity内的onStart()中,以查看它调用了多少次也就是Log
ProductCategories
activity
 <activity
        android:name=".ProductCategories"
        android:launchMode="singleTask"/>