简体   繁体   中英

Android startActivityForResult very slow

I am calling an activity using startActivityForResult(intent, 0); , and then setting the result of my activity when a calendarview is changed and then finish(); ing the activity. But when I call startActivityForResult(); it takes forever to load the activity.Here is how I launch the SecondActivity: in the oncreate:

calendar = new Intent(Intent.ACTION_GET_CONTENT).setClass(this, CalendarShow.class);

and on a fling gesture:

  @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2, 
                float velocityX, float velocityY) {
                                startActivityForResult(calendar, 0);

            return true;
        }

Here is code for the second activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
        setContentView(R.layout.activity_calendar_show);
        CalendarView v = new CalendarView(this);
        Intent intent = getIntent();
        v.setFocusedMonthDateColor(Color.BLACK);
        v.setUnfocusedMonthDateColor(Color.GRAY);
        v.setSelectedWeekBackgroundColor(Color.TRANSPARENT);
        v.setWeekNumberColor (Color.TRANSPARENT);
        v.setLayoutParams(new RelativeLayout.LayoutParams(400, 400));
        v.setId(99);
        v.setOnDateChangeListener(new OnDateChangeListener(){

            @Override
            public void onSelectedDayChange(CalendarView view, int year,
                    int month, int dayOfMonth) {
                Intent ret = new Intent();
            ret.putExtra("year", year);
            ret.putExtra("month", month);
            ret.putExtra("day", dayOfMonth);
            setResult(RESULT_OK, ret);
            finish();
            overridePendingTransition(R.anim.slide_in_up2, R.anim.slide_out_up2);
        }

    });
        RelativeLayout layout = (RelativeLayout)findViewById(R.id.calendarLayout);
        layout.addView(v);
    }

EDIT: I tried starting the activity just normal, without the ForResult and it was still slow.

好的,因此您不应该以挥动手势开始活动,并且这样做时加载时间很长,因此可以将活动更改为片段。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM