Issue
I try to to add an Splash Activity to my application this splash activity open when the program start for first time
but when the app work and skip the splash activity to main activity and user hold on and go to the home screen
after that the user will return to the app ,,
the problem is here so the application reopen the splash activity again
i don't want the app to open the splash activity every time the application resume activity i need to direct open the main activity ...
I tried this code but it doesn't work with me
In Splash Activity ...
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Class<?> activityClass;
try
{
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
activityClass = Class.forName(prefs.getString("lastActivity", MainActivity.class.getName()));
}
catch(ClassNotFoundException ex)
{
activityClass = MainActivity.class;
}
startActivity(new Intent(this, activityClass));
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
}
);
}
}
and in the Main Activity i use this code
public class Main2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
}
}
Solution
Use Application class to save a global flag:
public class App extends Application {
private static boolean sFirstRun = false;
public static boolean fetchFirstRun() {
boolean old = sFirstRun;
sFirstRun = false;
return old;
}
//--called when app process is created--
@Override
public void onCreate() {
super.onCreate();
sFirstRun = true;
}
}
And in manifest.xml add : <application android:name=".App"
now, App.fetchFirstRun()
is true only when App process is created from scratch. As long as app is running subsequent calls will return false.
Answered By - S.D.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.