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

xienkqul  于 2023-06-28  发布在  Android
关注(0)|答案(3)|浏览(106)

我是Android新手。
我有three activities A,B and C
activity B中,我有一个listView,其中包含项目列表。On click每个项目我想在第三个活动中显示项目详细信息。它工作正常。 但是,当在模拟器中单击第三个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);
        }
    }
pes8fvy9

pes8fvy91#

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

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

wtzytmuj2#

将此添加到您的活动:

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}
46qrfjad

46qrfjad3#

更正代码并添加以下内容以控制背部按压:

@override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

相关问题