How to pass intent extras to onactivityresult when activity reboot?

夙愿已清 提交于 2019-12-12 04:55:21

问题


I am writing an android application. Need to pass int value.I have a listview that it has contextmenu.I must start Intent.Action_Call. When I start my intent. My application is going to background. When intent is finish my application start a survey.My problem is ;I am not getting user"s clicked position.I tried insert to putExtra but in onActivityResult void does not accepted value of my user"s clicked position and value become zero.How can I get ıt ?

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

回答1:


It sounds like you're expecting to be able to call this:

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

and then later, when you receive the result:

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

Unfortunately, this is not how it works. The Intent data argument to onActivityResult() is not the same Intent that you used to start that activity. The activity that you started has complete control over whether the data intent is non-null, and what's inside of it.

Usually for public APIs, there is documentation about what you can expect to have inside the data intent.




回答2:


First of all, put this on onContextItemSelected

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

                editor.putInt("kelime", selectedPosition);

                editor.commit();

After Settle down above cod,Set onActivityResult

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

Finally, set the adapter same void :

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


来源:https://stackoverflow.com/questions/45246699/how-to-pass-intent-extras-to-onactivityresult-when-activity-reboot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!