Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: Instantly start an activity and then load content

Tags:

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.

That's what i got:

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

This is the activity I'm loading:

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.

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

Question: How to load setContentView() on the background?

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.

like image 498
John Sardinha Avatar asked Apr 03 '16 12:04

John Sardinha


People also ask

Can an activity start itself Android?

Yes it is. If your requirement are like that then there is no harm in doing that.

How do I start the same activity again on Android?

If you just want to reload the activity, for whatever reason, you can use this. recreate(); where this is the Activity. This is never a good practice. Instead you should startActivity() for the same activity and call finish() in the current one.

How do you start an activity only once the app is opened for the first time?

It is important to check that the first activity which opens when the app is launched is MainActivity. java (The activity which we want to appear only once). For this, open the AndroidManifest. xml file and ensure that we have the intent-filter tag inside the activity tag that should appear just once.

How do I pass data between activities in Android?

This example demonstrates how do I pass data between activities in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

Possible solution

  • 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).

  • 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.

like image 101
Hitesh Sahu Avatar answered Oct 01 '22 02:10

Hitesh Sahu


Your activity B should have AsyncTask for loading data and then displaying it. Also you can load some default data in view inside of OnPreExecute()

@Override protected void onCreate(Bundle savedInstanceState) {     setupViews(); }  private void setupViews(){     someView1 = (TextView) findViewById(R.id.some_view_id1);     someView2 = (TextView) findViewById(R.id.some_view_id2);     someView3 = (ImageView) findViewById(R.id.some_view_id3);     progressBar = (ProgressBar) findViewById(R.id.progress_bar_id);     new LoadDataForActivity().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }  private class LoadDataForActivity extends AsyncTask<Void, Void, Void> {  String data1; String data2; Bitmap data3;      @Override     protected void onPreExecute() {         progressBar.setVisibility(View.VISIBLE);     }     @Override     protected Void doInBackground(Void... params) {         data1 = loadData1();         data2 = loadData2();         data3 = loadData3();     }      @Override     protected void onPostExecute(Void result) {         someView1.setText(data1);         someView2.setText(data2);         someView3.setImageBitmap(data3);         progressBar.setVisibility(View.GONE);     }  } 
like image 43
Wackaloon Avatar answered Oct 01 '22 02:10

Wackaloon