如何在活动重启时将intent extras传递给onactivityresult?

时间:2017-07-21 21:05:30

标签: android android-intent onactivityresult

我正在编写一个Android应用程序。需要传递int值。我有一个listview,它有contextmenu.I必须启动Intent.Action_Call。当我开始我的意图。我的申请将转到后台。当意图完成我的应用程序开始调查。我的问题是;我没有得到用户点击的位置。我尝试插入putExtra但在onActivityResult中无法接受我的用户点击的位置值和值变为零。如何我能得到吗?

   public boolean onContextItemSelected(MenuItem item) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        selectedPosition = info.position;

     //in here , this value is true.
            Toast.makeText(AnaMenu.this, String.valueOf(selectedPosition), Toast.LENGTH_LONG).show();

            switch (item.getItemId()) {
                case MENU_Ara:

                    Intent i = getIntent();
                    final ArrayList<String> secilmis = i.getStringArrayListExtra("listem");

                    Intent ara = new Intent(Intent.ACTION_CALL);
                    ara.setData(Uri.parse("tel:" + secilmis.get(info.position).toString()));


                    ara.putExtra("selected", selectedPosition);





                    startActivityForResult(ara, AnketDegerlendirmesi);






                    return true;
            }


            return super.onContextItemSelected(item);
        }




     protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {

            if (requestCode == AnketDegerlendirmesi){
                Toast.makeText(AnaMenu.this,String.valueOf(selectedPosition),Toast.LENGTH_LONG).show();

                final AlertDialog.Builder  mBuilder = new AlertDialog.Builder(AnaMenu.this);
                mBuilder.setTitle("Deger");
                mBuilder.setIcon(R.drawable.ic_build_black_24dp);
                mBuilder.setMultiChoiceItems(afteractivitydialogitems, dialogcheckeditems, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int position, boolean isChecked) {



                        if (isChecked){
        mUserselecteditems.add(Integer.valueOf(dialogdegerleri[position]));
    }
    if (!isChecked) {
    if (mUserselecteditems.contains(Integer.valueOf(dialogdegerleri[position]))){
        mUserselecteditems.remove(Integer.valueOf(dialogdegerleri[position]));
    }
    }
     }
                });

                mBuilder.setCancelable(false);
                mBuilder.setPositiveButton("Tamamdır.", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        String items = "";
                        toplamaislemi = 0;
                        for (int item : mUserselecteditems) {
                            items += "-" + item + "\n";

                            toplamaislemi = toplamaislemi + item;



   //When i want to get this value.Value is always zero(0)
                        String returnString = data.getStringExtra("selected");
                        kullanıcısayıdegerlistesi.remove(String.valueOf(returnString));
                        kullanıcısayıdegerlistesi.add(Integer.valueOf(returnString),String.valueOf(toplamaislemi));
                        oyunTextView.notifyDataSetChanged();

                    }
                }  )             .show();


            }


            super.onActivityResult(requestCode, resultCode, data);
        }

2 个答案:

答案 0 :(得分:2)

听起来你似乎能够称之为:

Intent i = new Intent(...);
i.putExtra("some_key", someValue);
startActivityForResult(i, requestCode);

然后,当您收到结果时:

protected void onActivityResult(... Intent data) {
    data.getExtra("some_key"); // should be `someValue` from above
}

不幸的是,这不是它的工作原理。 Intent data的{​​{1}}参数与您用于启动该活动的onActivityResult()不同。您启动的活动可以完全控制Intent意图是否为非空,以及其中的内容。

通常对于公共API,有关于data意图中可能包含的内容的文档。

答案 1 :(得分:0)

首先,将它放在onContextItemSelected

SharedPreferences settings = getSharedPreferences("PREFS_NAME", MODE_PRIVATE);
                SharedPreferences.Editor editor = settings.edit();
editor.clear();

                editor.putInt("kelime", selectedPosition);

                editor.commit();

在鳕鱼上方定居后,设置onActivityResult

SharedPreferences settings = getSharedPreferences("PREFS_NAME", MODE_PRIVATE);
                    int selec = settings.getInt("kelime",selectedPosition);

最后,将适配器设置为void:

kullanıcısayıdegerlistesi.remove(selec);
                        kullanıcısayıdegerlistesi.add(selec ,String.valueOf(sonuc));