Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:51:10+00:00 2026-05-27T14:51:10+00:00

My Android app starts on an activity named LoadingActivity . This activity loads a

  • 0

My Android app starts on an activity named LoadingActivity. This activity loads a few files of my webserver. First it checks a timestamp in a text file on my server and checks if this timestamp is newer than one stored on the device. If it is, it loads the content of another file on my webserver and saves this to a file on the device.
When all this is done, I would like to go to another activity, MainActivity.
I do all this in the onWindowFocusChanged method like this:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    URL urlUpdated = null;
    BufferedReader in = null;
    try {
        urlUpdated = new URL("http://someurl.com/timestamp.txt");
        in = new BufferedReader(new InputStreamReader(urlUpdated.openStream()));

        String updated = "";
        String inputLine;

        while ((inputLine = in.readLine()) != null)
            updated = updated + inputLine;
        in.close();

        SharedPreferences preferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        String lastUpdated = preferences.getString("lastUpdated", "");

        if (!lastUpdated.equals(updated)) {
            // Download JSON file (schedule)
            String schedule = "";
            URL urlSchedule;
            urlSchedule = new URL("http://someurl.com/downloadMe.txt");
            in = new BufferedReader(new InputStreamReader(urlSchedule.openStream()));
            while ((inputLine = in.readLine()) != null)
                schedule = schedule + inputLine;
            Log.d(LOG_TAG, schedule);

            // Write schedule to file
            String PATH = "/data/data/com.codeinacup.NibeFestival/";    
            File file = new File(PATH + "Schedule.json");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream);
            writer.write(schedule);
            writer.close();
            fileOutputStream.close();

            // Update last updated preference
            editor.putString("lastUpdated", updated);
            editor.commit();
        }
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

    // Go to next MainActivity
    Intent scheduleIntent = new Intent(this, MainActivity.class);
    startActivity(scheduleIntent);
}

My problem is that when doing this, my LoadingActivity will be displayed but disappear again very fast and the screen will turn black for a couple of seconds and then MainActivity appears. Then, when the user press the back button, it will go back to MainActivity again. I found it obvious, that the back button should go back to LoadingActivity.

So my question is, how would I implement an activity for loading correctly? My idea of a loading activity is:

  • Displayed when the application launches
  • Downloads the files
  • Goes to the next activity, MainActivity
  • When at MainActivity and pressing the back button, the application should quit
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:51:10+00:00Added an answer on May 27, 2026 at 2:51 pm

    onWindowFocusChanged gets called when the Activity gains or loses focus. So you start your Activity and it calls onWindowFocusChanged, because it gains focus. When it’s finished loading your stuff it starts the MainActivity and loses focus again but executes the method again.

    What I would do is create an AsyncTask which loads your stuff and executes the task in the MainActivity. And it would be nice if you show the user something, so that he knows the app is loading something. ProgressDialog

    No need of 2 Activitys.

    Hope it helps.
    Regards
    Dominic

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I want is that when Activity starts first time, it first checks if
I'm building an Android app and I want to copy the text value of
I've got an Android app which has a periodic background Service. I want this
i've developed an android app that's fetches an xml file and displays this data
I am trying to develop my first Android app through the ADT plugins for
I am confirming about creating activity. My Manifest.xml is like this : <application android:icon=@drawable/icon
When I run an android app from eclipse I meet this error and my
I have an Android App with a number of activities. The wrong activity is
I have a tab layout app that needs to download a few files right
I have a few crash report logs from android market similar to this: Exception

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.