0

I have tried going through so many solution none worked for me. I have

Slpashscreen > MainActivity > other Activities ...

Now when i press

Enter first time it should

Slpashscreen > MainActivity > HOME BUTTON >  other Activities

Second time Resume Directly( Do not show me slash screen )

MainActivity > other Activities

Can anyone help me.

I tried using Intent.FLAG_ACTIVITY_SINGLE_TOP and tried launchMode="singleTask" for slpashscreen as well as main activity ( all possibility) still not working

Cœur
  • 37,241
  • 25
  • 195
  • 267
Manisha
  • 813
  • 13
  • 24
  • possible duplicate of [How do I make a splash screen in android](http://stackoverflow.com/questions/5486789/how-do-i-make-a-splash-screen-in-android) – Infinite Recursion Jun 11 '14 at 09:27

4 Answers4

0

its Splash Screen not slpashscreen i think.....here is one solution call the obj of the main activity into splash screen ans add splash screen into main method....use threads with splash screen ....

0

Define one flag (IS_SPLASHSCREEN_LAUNCHED default false) for SplashScreen and save it in either sharedpreference or in sqlite databse.

With in oncreate() of MainActivty check the flag (IS_SPLASHSCREEN_LAUNCHED), if it is false launch SplashScreen and set the flag to true and save it , After fev seconds finish the splashscreen. If it is true dont launch Splashscreen.

Note: You should finish splashscreen by calling SplashScreen.this.finish() in SplashScreen Activity.

Adarsh Gowda
  • 196
  • 2
  • 13
0

Here is the solution :

This is

public class SplashScreen extends Activity {

    private ImageView loading;  
    private TextView loadingTx;
    private AnimationDrawable loadAnimation;

    private SharedPreferences prefs;
    private String prefName = "MyPref";

    private static final String  IS_SPLASHSCREEN_LAUNCHED_STR="Is_Slpash_Screen_launched";
    private static final String THROUGH_LAUNCH_SCREEN_STR ="Trhough_launch_Screen";



    private String TAG = SplashScreen.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        RelativeLayout loading_screen_layout = new RelativeLayout(this);

        DisplayMetrics display = this.getResources().getDisplayMetrics();

        int screen_width = display.widthPixels; 
        int screen_height = display.heightPixels; 

        RelativeLayout.LayoutParams loading_params = new RelativeLayout.LayoutParams(100, 100);
        loading_params.leftMargin = (screen_width/2)-50; // to put this exactly on center we are going half the width of screen minus half the width of Roter
        loading_params.topMargin = (screen_height*2)/3; //two third down from top

        RelativeLayout.LayoutParams loadingTx_params = new RelativeLayout.LayoutParams(130, 100);
        loadingTx_params.leftMargin = (screen_width/2)-50;// to put this exactly on center we are going half the width of screen minus half the width of Roter
        loadingTx_params.topMargin = (screen_height*2)/3 +100;//two third down from top plus the height of roter

        setContentView(loading_screen_layout);
        loading_screen_layout.setBackgroundResource(R.drawable.launch_screen);

        loading = new ImageView(this);

        loadingTx = new TextView(this);
        loadingTx.setText("Loading...");
        loading.setImageResource(R.drawable.loading);
        loadAnimation = (AnimationDrawable)loading.getDrawable();

        loading_screen_layout.addView(loading, loading_params);
        loading_screen_layout.addView(loadingTx,loadingTx_params);


        /*
         * Showing splashscreen while making network calls to download necessary
         * data before launching the app Will use AsyncTask to make http call
         */
        new PrefetchData().execute();


    }

    private class PrefetchData extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // before making http calls
            //Log.e("JSON", "Pre execute");
        }

        @Override
        protected Void doInBackground(Void... arg0) {

            //getting only first 10 results from JSON scrolling
            JSONPageResults.getFirstPageResults();

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // After completing http call
            // will close this activity and lauch main activity
            Intent i = new Intent(SplashScreen.this, MainAppActivity.class);

            i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);

                     //****************This is where i have used Shared Preference *****

            prefs = getSharedPreferences(prefName, MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean(IS_SPLASHSCREEN_LAUNCHED_STR, true);
            editor.putBoolean(THROUGH_LAUNCH_SCREEN_STR, true);
            editor.commit();

            finish();
        }
    }

//  //can't start animating in OnCreate, so start when window becomes in focus
    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        loadAnimation.start();
    }
}

While in Main Activity i retrieved information in onCreate()

public class MainAppActivity extends Activity {


private SharedPreferences prefs;
private String prefName = "MyPref";

private static boolean IS_SPLASHSCREEN_LAUNCHED;

private static boolean THROUGH_LAUNCH_SCREEN;

private static final String  IS_SPLASHSCREEN_LAUNCHED_STR = "Is_Slpash_Screen_launched";
private static final String THROUGH_LAUNCH_SCREEN_STR ="Trhough_launch_Screen";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);

    Resources res = getResources();
    Log.i(TAG,"Activity Created ! ");

    //onNewIntent(getIntent());

    prefs = getSharedPreferences(prefName, MODE_PRIVATE);
    IS_SPLASHSCREEN_LAUNCHED = prefs.getBoolean(IS_SPLASHSCREEN_LAUNCHED_STR, false);
    THROUGH_LAUNCH_SCREEN = prefs.getBoolean(THROUGH_LAUNCH_SCREEN_STR, false);
    if(!IS_SPLASHSCREEN_LAUNCHED)
    {
        Intent launchSlpash =  new Intent(this, SplashScreen.class);
        startActivity(launchSlpash);
        return;
    } 
}

I have also made MainAppActivity LAUNCHER Activity and then opening Splashscreen activity if not opned.

</activity>
        <activity
        android:name="com.mvyas.amazon.SplashScreen"
        android:label="@string/app_name"
        android:screenOrientation="landscape" 
         >

    </activity>

    <activity
        android:name="com.mvyas.amazon.MainAppActivity"
        android:screenOrientation="landscape"
        android:launchMode="singleTask"
         >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Manisha
  • 813
  • 13
  • 24
0

I recommend to write seperate class for saving and retrieving data like this :

import android.content.SharedPreferences;

    public class PreferencesData {

        private SharedPreferences prefs;
        private String prefName = "MyPref";

        public static final String  IS_SPLASHSCREEN_LAUNCHED_STR      = "Is_Slpash_Screen_launched";
        public static final String THROUGH_LAUNCH_SCREEN_STR ="Trhough_launch_Screen";

        public static void putBoolean(String ID) {
            // write code for putting data into sharedPreference
        }

        public static boolean getBoolean(String ID) {
            // write code for getting data from sharedPreference
        }

    }
Adarsh Gowda
  • 196
  • 2
  • 13