我在onActivityResult上遇到问题。关于此的话题已经有好几个了,但是我所看到的建议中没有一个对我有用。

我有以下活动EditDate:

public class EditDate extends AppCompatActivity {
    EditDateFragment editDateFragment;
    JSONObject usrObject;
    int YEARPOSITION = -1;
    int MONTHPOSITION = -1;
    static final int CHOOSEDAY = 1;
    JSONArray resultsOld;
    JSONArray maxResultsOld;
    JSONArray disciplinesOld;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_date);
        usrObject = MainActivity.usrObject;
        Intent intent = getIntent();
        YEARPOSITION = intent.getIntExtra("YEARPOSITION",0);
        MONTHPOSITION = intent.getIntExtra("MONTHPOSITION",0);
        editDateFragment = new EditDateFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.edit_date_container,editDateFragment).commit();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        MainActivity.showToast("WTF!");
    }
    }


和一个片段EditDateFragment:

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * A placeholder fragment containing a simple view.
 */
public class EditDateFragment extends ListFragment {
    ListView mainListView;
    InteractiveArrayAdapter listAdapter;
    String LOG_TAG = getClass().getName();
    int YEARPOSITION = -1;
    int YEAR = -1;
    int MONTHPOSITION = -1;
    int MONTH = -1;
    private static final int CHOOSEDAY = -1;
    private float x1, x2;
    static final int MIN_DISTANCE = 150;
    GestureDetection gestureDetection;
    private JSONArray DAYS;

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        Intent intent = getActivity().getIntent();
        YEARPOSITION = intent.getIntExtra(MainActivity.YEARPOS, MainActivity.getStartYear());
        MONTHPOSITION = intent.getIntExtra(MainActivity.MONTHPOS, MainActivity.getStartMonth());
        gestureDetection = new GestureDetection();
        mainListView = getListView();
        mainListView.setOnTouchListener(gestureDetection);
        mainListView.setOnItemClickListener(listener);
        print();
    }
    public EditDateFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_edit_date, container, false);
        gestureDetection = new GestureDetection();
        ViewListener viewListener = new ViewListener();
        rootView.setOnTouchListener(viewListener);
        return rootView;
    }

    public void print() {
        try {
            ArrayList<Model> testResults = getResults();
            for (int i = 0; i < testResults.size(); i++) {
                System.out.println(testResults.get(i).getName());
            }
            mainListView = getListView();
            listAdapter = new InteractiveArrayAdapter(this.getActivity(), testResults);

            if (mainListView == null) {
            } else {
                mainListView.setAdapter(listAdapter);
            }
        } catch (JSONException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
            e.printStackTrace();
        }
    }

    private ArrayList<Model> getResults() throws JSONException {

        ArrayList<Model> tmp = new ArrayList<Model>();

        try {

            JSONArray list = MainActivity.usrObject.getJSONArray(MainActivity.LIST);

            JSONObject yearObj = list.getJSONObject(YEARPOSITION);
            YEAR = yearObj.getInt(MainActivity.LIST_YEAR);
            String year = Integer.toString(yearObj.getInt(MainActivity.LIST_YEAR));
            JSONArray months = yearObj.getJSONArray(MainActivity.LIST_MONTHS);

            JSONObject monthObject = months.getJSONObject(MONTHPOSITION);
            MONTH = monthObject.getInt(MainActivity.MONTH);
            String month = Integer.toString(monthObject.getInt(MainActivity.MONTH));
            JSONArray days = monthObject.getJSONArray(MainActivity.LIST_DAYS);
            DAYS = days;
            for (int k = 0; k < days.length(); k++) {
                JSONObject dayObject = days.getJSONObject(k);
                String day = Integer.toString(dayObject.getInt(MainActivity.DAY));
                StringBuilder sb = new StringBuilder();
                sb.append(day + "/" + month + "  " + year);
                tmp.add(get(sb.toString()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //return tmp;

        return tmp;
    }

    private Model get(String s) {
        return new Model(s);
    }

    AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            if (gestureDetection.swipeDetected()) {
                if (gestureDetection.getAction() == GestureDetection.Action.RL) {
                    int tmpMonthPos = MONTHPOSITION + 1;
                    try {
                        int maxMonth = getMaxMonth(YEARPOSITION);
                        if (tmpMonthPos > maxMonth) {
                            int tmpYearPos = YEARPOSITION + 1;
                            int maxYear = getMaxYear();
                            if (tmpYearPos <= maxYear) {
                                tmpMonthPos = 0;
                                startNewActivity(tmpYearPos,tmpMonthPos);
                            }
                        } else {
                            startNewActivity(YEARPOSITION,tmpMonthPos);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
                if (gestureDetection.getAction() == GestureDetection.Action.LR){

                    int tmpMonthPos = MONTHPOSITION -1;
                    if (tmpMonthPos < 0){
                        int tmpYearPos = YEARPOSITION - 1;
                        if ( tmpYearPos >= 0){
                            try {
                                int monthPos = getNewMonthPos(tmpYearPos);

                                startNewActivity(tmpYearPos,monthPos);

                            } catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    } else {
                        startNewActivity(YEARPOSITION, tmpMonthPos);
                    }
                }
            }
            else {


                startCalendar(position);


            }
        };
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        System.out.println("HERE HERE");
        if (requestCode == CHOOSEDAY){
            if (resultCode == getActivity().RESULT_OK){
                Bundle bundle = data.getExtras();
                int day = bundle.getInt(MainActivity.DAY);
                int month = bundle.getInt(MainActivity.MONTH);
                int year = bundle.getInt(MainActivity.LIST_YEAR);
                MainActivity.showToast(day + " / " + month + " - " + year);
            }
        }
    }


    private void startCalendar(int position){
        MainActivity.showToast("HERE AT LEAST");
        Intent intent = new Intent(this.getContext(),showCalendar.class);
        intent.putExtra(MainActivity.YEARPOS,YEARPOSITION);
        intent.putExtra(MainActivity.MONTHPOS, MONTHPOSITION);
        startActivityForResult(intent, CHOOSEDAY);
    }


    private void startNewActivity(int yearposition, int monthposition) {
        Intent intent = new Intent(this.getActivity(), EditDate.class);
        intent.putExtra(MainActivity.YEARPOS, yearposition);
        intent.putExtra(MainActivity.MONTHPOS, monthposition);
        startActivity(intent);
    }

    private int getNewMonthPos(int yearPos) throws JSONException {
        JSONArray list = MainActivity.usrObject.getJSONArray(MainActivity.LIST);
        JSONObject yearObj = list.getJSONObject(yearPos);
        JSONArray tmpMonths = yearObj.getJSONArray(MainActivity.LIST_MONTHS);
        return tmpMonths.length() - 1;

    }

    private int getMaxYear() throws JSONException {
        JSONArray list = MainActivity.usrObject.getJSONArray(MainActivity.LIST);
        return list.length() - 1;
    }

    private int getMaxMonth(int yearPos) throws JSONException {
        JSONArray list = MainActivity.usrObject.getJSONArray(MainActivity.LIST);
        JSONObject yearObj = list.getJSONObject(yearPos);
        JSONArray months = yearObj.getJSONArray(MainActivity.LIST_MONTHS);
        return months.length() - 1;
    }

    public class ViewListener implements View.OnTouchListener {


        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x1 = event.getX();
                    break;
                case MotionEvent.ACTION_UP:
                    x2 = event.getX();
                    float deltaX = x2 - x1;

                    if (Math.abs(deltaX) > MIN_DISTANCE) {
                        // Left to Right swipe action
                        if (x2 > x1) {

                            int tmpMonthPos = MONTHPOSITION -1;
                            if (tmpMonthPos < 0){
                                int tmpYearPos = YEARPOSITION - 1;
                                if ( tmpYearPos >= 0){
                                    try {
                                        int monthPos = getNewMonthPos(tmpYearPos);

                                        startNewActivity(tmpYearPos,monthPos);

                                    } catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                            } else {
                                startNewActivity(YEARPOSITION, tmpMonthPos);
                            }
                        }

                        // Right to left swipe action
                        else {

                            int tmpMonthPos = MONTHPOSITION + 1;
                            try {
                                int maxMonth = getMaxMonth(YEARPOSITION);
                                if (tmpMonthPos > maxMonth) {
                                    int tmpYearPos = YEARPOSITION + 1;
                                    int maxYear = getMaxYear();
                                    if (tmpYearPos <= maxYear) {
                                        tmpMonthPos = 0;
                                        startNewActivity(tmpYearPos,tmpMonthPos);
                                    }
                                } else {
                                    startNewActivity(YEARPOSITION,tmpMonthPos);
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } else {

                    }
                    break;
            }
            return true;
        }

    }

}


当我单击ListFragment中的项目时,将显示一个日历,我从函数startCalendar(int position)开始

用于显示日历的活动和片段是
活动:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class showCalendar extends AppCompatActivity {
    showCalendarFragment showCalendarFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_calendar);
        showCalendarFragment = new showCalendarFragment();
        this.getSupportFragmentManager().beginTransaction().add(R.id.show_calendar_container,showCalendarFragment).commit();
    }
}


showCalendarFragment:

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;

/**
 * A placeholder fragment containing a simple view.
 */
public class showCalendarFragment extends Fragment implements CalendarView.OnDateChangeListener {

    private CalendarView calendarView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        Intent intent = getActivity().getIntent();

        calendarView = (CalendarView)getView().findViewById(R.id.calendarView);
        calendarView.setOnDateChangeListener(this);
    }
    public showCalendarFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_show_calendar, container, false);
    }

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        month = month  + 1;

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt(MainActivity.LIST_YEAR,year);
        bundle.putInt(MainActivity.MONTH,month);
        bundle.putInt(MainActivity.DAY,dayOfMonth);
        intent.putExtras(bundle);
        getActivity().setResult(getActivity().RESULT_OK,intent);
        getActivity().finish();
    }

}


当我单击列表中的项目时,日历即会启动并按其显示。选择日期后,活动将停止并返回到EditDate活动。但是,无论我做什么,我都不会看到onActivityResult被调用。不在EditDate活动中,也不在EditDateFragment中。我尝试了一些不同的方法,例如尝试getActivity.startActivityForResult(intent,requestcode)和使用super.onActivityStarted(requestcode, resultCode,intent)等。没事。

理想情况下,我希望调用Activity中的onActivityResult()。任何帮助表示赞赏!

编辑:现在工作,我在EditDateFragment中具有静态最终int CHOOSEDAY = -1,在EditDate中具有CHOOSEDAY = 1。

最佳答案

仅当您使用startActivityForResult()启动新活动而不是片段时才调用此方法!我认为这对您有帮助

Is there a method that works like start fragment for result?

关于android - 在Activity和Fragment中都不调用onActivityResult,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34300586/

10-11 13:45