Android on activity result always return 0 and null intent
Asked Answered
L

2

0

I tried almost all solutions found from the net and still can't solve my problem. Would anyone please help with the following codes? I really can't make it work. Even if I tried to put the setresult code in onBackPressed event, the parent activity still get result code = 0.

  1. Start activity for result in the parent [CreatePostActivity] on touch event of a spinner:

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        mContext = this;
        appState = ((ApplicationProvider) getApplicationContext());

    LayoutInflater inflate = LayoutInflater.from(this);
    mContent = inflate.inflate(R.layout.activity_create_post, null);
    setContentView(mContent);

    spinnerCategory = (Spinner) mContent.findViewById(R.id.spinnerCategory);
    spinnerCategory.setOnTouchListener(this);

    arrayCategory = new String[] { getResources().getString(R.string.create_post_category) };
    adapterCategory = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrayCategory);
    spinnerCategory.setAdapter(adapterCategory);}


@Override
public boolean onTouch(View v, MotionEvent event) {

    if (v.getId() == R.id.spinnerCategory) {
        Bundle bundle = new Bundle();
        bundle.putInt("CategoryId", categoryId);
        Intent intent = new Intent(this, CreatePostActivity_Category.class);
        intent.putExtras(bundle);
        // intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, Variables.CREATEPOST_CATEGORY_ACTIVITY_REQUEST_CODE);
    }
    return true;
}
  1. Child [CreatePostActivity_Category] contains a listview and initialize with adapter like below:

listitem_select_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:padding="5dp" >

    <TextView
        android:id="@+id/textViewItem"
        style="@style/Heading.h2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="TextView" />

    <CheckBox
        android:id="@+id/checkBoxItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

CreatePostActivity to receive :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Variables.CREATEPOST_CATEGORY_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            System.out.println("OK");
        } else
            System.out.println("CANCEL");
    }
}

CreatePostActivity_Category:

public class CategoryAdapter extends BaseAdapter implements OnCheckedChangeListener {
    private LayoutInflater inflater = null;

    public CategoryAdapter(CategoryModel[] list) {
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mListItems.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.listitem_select_checkbox, null);

        TextView textViewItem = (TextView) vi.findViewById(R.id.textViewItem);
        textViewItem.setText(mListItems[position].getDescription((String) appState.getPreference(Variables.PREF_LANG)));

        CheckBox checkBoxItem = (CheckBox) vi.findViewById(R.id.checkBoxItem);
        if (categoryId == mListItems[position].CategoryId)
            checkBoxItem.setChecked(true);
        checkBoxItem.setTag(R.id.tag_id, mListItems[position].CategoryId);
        checkBoxItem.setTag(R.id.tag_desc, mListItems[position].getDescription((String) appState.getPreference(Variables.PREF_LANG)));
        checkBoxItem.setOnCheckedChangeListener(this);

        return vi;
    }

    @Override
    public void onCheckedChanged(CompoundButton v, boolean isChecked) {
        if (isChecked) {
            int id = Integer.parseInt(((CompoundButton) v).getTag(R.id.tag_id).toString());
            String desc = ((CompoundButton) v).getTag(R.id.tag_desc).toString();

            Bundle bundle = new Bundle();
            bundle.putInt("CategoryId", id);
            bundle.putString("Category", desc);
            Intent mIntent = new Intent();
            mIntent.putExtras(bundle);

            if (getParent() == null) {
                setResult(Activity.RESULT_OK, mIntent);
            } else {
                getParent().setResult(Activity.RESULT_OK, mIntent);
            }
            finish();
        }
    }
}



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mContext = this;
    appState = ((ApplicationProvider) getApplicationContext());
    database = DatabaseHelper.instance();

    LayoutInflater inflate = LayoutInflater.from(this);
    mContent = inflate.inflate(R.layout.activity_create_post_category, null);
    setContentView(mContent);

    ImageButton ibtnClose = (ImageButton) mContent.findViewById(R.id.ibtnClose);
    ibtnClose.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            finish();
        }
    });

    listViewCategory = (ListView) mContent.findViewById(R.id.listViewCategory);
    Cursor cr = database.select("SELECT category_id, edesc, cdesc FROM category");
    mListItems = new CategoryModel[cr.getCount()];

    if (cr != null) {
        if (cr.moveToFirst()) {
            int i = 0;
            do {
                int categoryId = cr.getInt(cr.getColumnIndex("category_id"));
                String cdesc = cr.getString(cr.getColumnIndex("cdesc"));
                String edesc = cr.getString(cr.getColumnIndex("edesc"));
                mListItems[i] = new CategoryModel(categoryId, cdesc, edesc);
                i++;
            } while (cr.moveToNext());
        }
    }
    cr.close();

    CategoryAdapter mCategoryAdapter = new CategoryAdapter(mContext, mListItems);
    listViewCategory.setAdapter(mCategoryAdapter);

    mData = this.getIntent().getExtras();
    categoryId = mData.getInt("CategoryId");
}
Lette answered 29/8, 2013 at 9:26 Comment(11)
Where is the code for finish child activity? share code of CreatePostActivity_CategoryMcgrath
it is commented inside the getview method in the adapter.Lette
why that is commented? You should replace the whole listner from adapter into activityMcgrath
Why its commented? setResult is essential.Ritchie
I didn't comment it in my code, I just want to make it clear in the question.Lette
I also tried to move the listener out but it didn't work.Lette
There are too many things which you removed from code before put here.. it would be easy if you add your full adapter code...Mcgrath
just updated. put the whole adapter , please check and help.Lette
what is mContext? Where did you initialised...Mcgrath
mContext = this; initialize in onCreateLette
see added answer.. and tell me if problemMcgrath
L
0

Finally I figured it out!!!

The original on touch listener I wrote has been fired three times, that's why it was unable to retrieve the actual return result.

Solved it by this --> Setting a spinner onClickListener() in Android

private View.OnTouchListener Spinner_OnTouch = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {

            if (v.getId() == R.id.spinnerCategory) {
                Bundle bundle = new Bundle();
                bundle.putInt("CategoryId", categoryId);
                Intent intent = new Intent(mContext, CreatePostActivity_Category.class);
                intent.putExtras(bundle);
                startActivityForResult(intent, Variables.CREATEPOST_CATEGORY_ACTIVITY_REQUEST_CODE);
            }
        }
        return true;
    }
};

Everything works. Thanks all for the suggested and correct answers.

Lette answered 30/8, 2013 at 2:13 Comment(0)
D
1

Before call the finish() method in your child activity,add the below code:

setResult(RESULT_OK);

Fllowing is new: 1、Add new variable:

Context mContext;

2、Change your adapter constructor:

public CategoryAdapter(Context context,CategoryModel[] list) {
    mContext = context;
    inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

3、Change finish() in adapter to:

((Activity)mContext).finish();

4、Replace your adapter's definition in your parent activity with the construction mentioned in Step 2

Dissonant answered 29/8, 2013 at 9:30 Comment(1)
try step 1、2、3、4 @WinnieLDissonant
L
0

Finally I figured it out!!!

The original on touch listener I wrote has been fired three times, that's why it was unable to retrieve the actual return result.

Solved it by this --> Setting a spinner onClickListener() in Android

private View.OnTouchListener Spinner_OnTouch = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {

            if (v.getId() == R.id.spinnerCategory) {
                Bundle bundle = new Bundle();
                bundle.putInt("CategoryId", categoryId);
                Intent intent = new Intent(mContext, CreatePostActivity_Category.class);
                intent.putExtras(bundle);
                startActivityForResult(intent, Variables.CREATEPOST_CATEGORY_ACTIVITY_REQUEST_CODE);
            }
        }
        return true;
    }
};

Everything works. Thanks all for the suggested and correct answers.

Lette answered 30/8, 2013 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.