kiran
kiran

Reputation: 3264

How to change initial screen?

I am new to android and I am facing a problem in my development. First my app starts with one screen(first screen).

Then it will move to another screen(second screen).My requirement is: when I restart my application ,it should starts from the second screen.

If any one has know how to handle this, please let me know.

Upvotes: 0

Views: 1705

Answers (6)

user3705260
user3705260

Reputation: 1

I had this same issue. I am pretty new to Android Development - so my answer may be flawed, but this is what worked for me.

Go to your AndroidManafest.xml file. Your activity definition section should look something like this:

   ...

   <activity
        android:name="com.example.test.OriginalFirstScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.test.NewFirstScreen"
        android:label="@string/title_activity_new_first_screen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    ...

I removed the tag from the OldFirstScreen Activity definition and left/added it in the NewFristScreen definition. This did the trick. See Below -

   ...

   <activity
        android:name="com.example.test.OriginalFirstScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

        </intent-filter>
    </activity>
    <activity
        android:name="com.example.test.NewFirstScreen"
        android:label="@string/title_activity_new_first_screen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    ...

Upvotes: 0

use share preference. 1. fetch boolean var from shared pref and check whether it is true or false. 2. if it is false , show first screen , set variable true and save variable in shared preferences. else show second screen

Upvotes: 0

Vibhuti
Vibhuti

Reputation: 704

It is not possible to change the default activity once it is set in manifest. What you can do is have a default activity at the start of the app(possibly like a splash screen). Have this code in your default activity

private SharedPreferences appPreferences = getSharedPreferences("user", MODE_PRIVATE);
final SharedPreferences.Editor editor = appPreferences.edit();
            editor.putBoolean("flag", true);
editor.commit();

if(editor.getBoolean("flag",false))
{
startActivity(new Intent(DefaultActivity.this,Activity1.class));
}
else
{
startActivity(new Intent(DefaultActivity.this,Activity2.class));
}

Now in your Activity1 make this boolean false

Upvotes: 0

anddev
anddev

Reputation: 3204

Hello kiran I think you dont want to start your first screen at every time. So for that you have to set the value in SharedPreferences when your first screen will display at very first time. Then every time you have to check if there is any value in sharedPreferences or not? If there is any value then start second screen otherzise first screen.

I tried this and it is worked.

EDIT:-

I used this in my application in that my requirements was I have to open registration screen only once/ only first time. Then in splash screen I write,

package com.z.z.z;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SplashActivity extends Activity
{   
    RelativeLayout lerSplash;
    SharedPreferences sharedPreferences;
    private String strUName_pref;
    private String TAG = "SplashActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Log.d(TAG, "in onCreate" );

        lerSplash = (RelativeLayout)findViewById(R.id.lerSplash);

        Handler handler = new Handler();
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, 3000L);

        sharedPreferences = this.getSharedPreferences("PREFERENCE_NAME",MODE_WORLD_READABLE);

    }

    Runnable runnable = new Runnable() 
    {
        public void run() 
        {
            strUName_pref = sharedPreferences.getString("PREFERENCE_NAME", null);

            if(strUName_pref != null)
            {
                Intent intent = new Intent(SplashActivity.this,MainTabActivity.class);
                startActivity(intent);
                finish();
            }
            else
            {
                Intent intent = new Intent(SplashActivity.this,RegistrationActivity.class);
                startActivity(intent);
                finish();
            }
        }
    };

}

Then in RegistrationActivity I write

package com.z.z.z;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RegistrationActivity extends Activity
{   

    SharedPreferences sharedPreferences;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration);
        Log.d(TAG, "in onCreate" );

        sharedPreferences = this.getSharedPreferences("PREFERENCE_NAME",MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = sharedPrefs.edit();
        prefsEditor.putString("SharedPreferenceValue", "YOUR_VALUE_WHICH_YOU_WANT_TO_PUT");
        prefsEditor.commit();

    }


}

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234857

You will need to store a flag somewhere that the first screen has been seen. The best place to do this, I think, is in your applications shared preferences. (See the guide topic Data Storage)

Then you need to use the flag. Here's one way to do it. Make the second screen your default launch activity. In it's onCreate method, check whether the flag is set and, if not, call startActivityForResult for the first screen, using a request code >= 0. Regardless of the value of the flag, finish onCreate for the second activity normally.

Have your first activity set a result code of success in its onCreate method. Then, back in your second activity, override onActivityResult so that when it receives a success result for the request code, it sets the flag.

This works because if you call startActivityForResult from the second activity, the screen will show the first activity without first flashing the second. Also, if the first activity crashes for some reason, the second activity will not receive the success result code and the flag will not be set, so the user will still see the first activity.

Here's some sample code:

First activity:

private static final String PREFS_NAME = "prefs";
private static final String KEY_SHOW = "show1";
private static final int REQUEST_1 = 0;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    if (settings.getBoolean(KEY_SHOW, true)) {
        Intent intent = new Intent(this, Activity1.class);
        startActivityForResult(intent, REQUEST_1);
    }
    . . . // the rest of onCreate as normal
}

protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_1 && resultCode == RESULT_SUCCESS) {
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(KEY_SHOW, true);
        editor.commit();
    }
}

Second activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setResult(RESULT_SUCCESS);
    . . .
}

Upvotes: 2

deepak Sharma
deepak Sharma

Reputation: 1641

What do you want exactly ?

if you want that your app starts every time where you exit from your app then you have to store a value in SharedPreference and upgrade it in every screen and on Restart/run you can check that value and starts that activity .

Else you have also another option posted above by my friends... Try that one.

Upvotes: 0

Related Questions