Android后退按钮不适用于多个活动

问题描述 投票:-12回答:2

我是android的新手。

我有three activities A,B and C

In the activity B我有一个listView列表中的item.On click每个项目我想要显示item details in the third activity.It工作正常。

但是,当在模拟器中单击第三个活动的后退按钮时,它向我显示我的应用程序已停止工作。

我无法重定向到第二个活动。

from the second activity我能来back to the first activity

我该如何解决这个问题?

在活动C中

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String displayTitle = extras.getString("title");
        String displayPrice = extras.getString("price");
        String BasicInfo = extras.getString("BasicInfo");
        try {
            BasicInfoobj = new JSONObject(BasicInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        txtTitle = (TextView)findViewById(R.id.display_txtTitle);
        txtPrice = (TextView)findViewById(R.id.display_txtPrice);
        txtLocation =(TextView)findViewById(R.id.display_txtLocation);
        txtTitle.setText(displayTitle);
        txtPrice.setText(displayPrice);

    }

在活动B中

private class myListAdapter extends ArrayAdapter<Items> {
        public myListAdapter()
        {
            super(ResultActivity.this,R.layout.item_view,itemList);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String CurrentImage="";
            View itemView = convertView;
            if(itemView == null)
            {
                itemView = getLayoutInflater().inflate(R.layout.item_view,parent,false);
            }

            //populate the list
            //find the item to work with
            final Items CurrentItem = itemList.get(position);
            //fill the view

            TextView title =(TextView)itemView.findViewById(R.id.txt_imageTitle);
            title.setText(CurrentItem.GetItemTitle());

            CurrentImage = CurrentItem.GetImgUrl();
            ImageView imageView = (ImageView)itemView.findViewById(R.id.item_icon);
            new ImageLoadTask(CurrentImage, imageView).execute();

            TextView Price =(TextView)itemView.findViewById(R.id.txt_itemPrice);
            Price.setText(CurrentItem.GetPrice());

            title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(ResultActivity.this,DisplayActivity.class);
                    Bundle extras = new Bundle();
                    extras.putString("title",CurrentItem.GetItemTitle());
                    extras.putString("price",CurrentItem.GetPrice());
                    extras.putString("basicURL",CurrentItem.GetImgUrl());
                    extras.putString("ImageUrl",CurrentItem.GetImgSuperUrl());
                    extras.putString("BasicInfo",CurrentItem.GetBasicInfo().toString());
                    extras.putString("SellerInfo",CurrentItem.GetSellerInfo().toString());
                    extras.putString("ShippingInfo",CurrentItem.GetShippingInfo().toString());
                    intent.putExtras(extras);
                    startActivity(intent);
                }
            });

            return itemView;
            //return super.getView(position, convertView, parent);
        }
    }
android back-button
2个回答
0
投票

在活动C中添加适当的代码

@Override
public void onBackPressed() 
{
    super.onBackPressed();
    moveTaskToBack(true);
}

试试这个


0
投票

将其添加到您的活动中。

@Override
public void onBackPressed() 
{
    super.onBackPressed();
    finish();
}
© www.soinside.com 2019 - 2024. All rights reserved.