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>