android:立即启动活动,然后加载内容 [英] android: Instantly start an activity and then load content

查看:83
本文介绍了android:立即启动活动,然后加载内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经调查了其他问题,博客和文档,但似乎找不到适合我需求的正确答案. 我有两个活动A和B.当我从A启动活动B时,我希望它立即打开,然后在显示进度条的同时加载所有内容,而不是仅在加载内容后才打开该活动好像冻结了两秒钟. 例如,Youtube应用或Play商店.

I've looked into other questions, blogs and the documention and can't seem to find the right answer to my needs. I have two activities, A and B. When I start activity B (from A) I want it to open instantly and then load all the content while showing a progress bar, instead of only opening the activity when the content is loaded, making it seem like it froze for two seconds. An exemple would be the Youtube app or the Play Store.

这就是我得到的:

Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent goB = new intent(ActivityA.this, ActivityB.class);
            startActivity(goB);
        }
    });

这是我正在加载的活动:

public class ActivityB extends AppCompatActivity implements OnDateSelectedListener, OnMonthChangedListener {

    private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();

    @Bind(R.id.calendarView) MaterialCalendarView widget;
    @Bind(R.id.textView) TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c_calendar);
        ButterKnife.bind(this);
        widget.setOnDateChangedListener(this);
        widget.setOnMonthChangedListener(this);
        textView.setText(getSelectedDatesString());

    }

    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @Nullable CalendarDay date, boolean selected) {
        textView.setText(getSelectedDatesString());
    }

    private String getSelectedDatesString() {
        CalendarDay date = widget.getSelectedDate();
        if (date == null) {
            return "No Selection";
        }
        return FORMATTER.format(date.getDate());
    }

    @Override
    public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {

    }

}

我不是专家,所以欢迎您提供详细的解释.

I'm not an expert, so detailed explanations will be welcomed.

注意:我在活动中正在加载的是此日历: https://github.com/prolificinteractive/material-calendarview

Note: What I'm loading in the activity is this calendar: https://github.com/prolificinteractive/material-calendarview

问题:如何在后台加载setContentView()?

更新:我遵循了Hitesh Sahu的建议,现在我只有一个活动,只有一个容器被替换为每个片段,我假设在后台加载xml内容的方法是片段和活动都一样,但是如果有区别,请提及.

Update: I followed Hitesh Sahu's advice and now I only have one activity with one container that gets replaced for each fragment, I'm assuming the way to load the xml content in the background will be the same for a fragment and an activity, but if there is any difference please do mention.

推荐答案

可能的解决方案

  • 我不是专家" 中,我猜测您可能会在每次屏幕切换时创建新活动,并且可能不会清除较旧的活动或重复使用现有活动.再次,这是我的猜测,我没有看到您的代码,但是我看到开发人员犯了这种错误.很快,您的App的性能会随着时间的流逝而下降,并且最终您的App会耗尽内存.

  • From "I am not an expert" my guess is you might be creating new activity on every screen switching and possibly not clearing older one or reusing existing activity. Again this is my guess I have not seen your code but I have seen developers doing this kind of mistakes .Soon your App's performance will degrade over time and your app will end up eating memory.

如何解决此问题:-用片段替换活动片段比进行转换活动要快,而且您可以重复使用它们(有关此内容的更多信息).

How to fix this :- Replace activities with fragment switching fragment is relatively faster than switching activities plus you can reuse them (more about this).

您可能正在Main Ui线程上执行一些繁重的任务,以查找编排器日志--skipped n number of frames app may be doing too much work on main thread

You might be doing some heavy task on Main Ui thread look for choreographer logs like - skipped n number of frames app may be doing too much work on main thread

如何解决此问题:-已在其他答案中进行了解释.

How to fix this:- already explained in other answers.

这篇关于android:立即启动活动,然后加载内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆