每次都会启动Splashscreen活动

时间:2014-06-11 08:43:24

标签: android-intent

我尝试过这么多解决方案,但对我来说都没有用。我有

Slpashscreen > MainActivity > other Activities ...

现在我按

首次输入

Slpashscreen > MainActivity > HOME BUTTON >  other Activities

第二次直接恢复(不要向我显示斜杠画面)

MainActivity > other Activities

任何人都可以帮助我。

我尝试过使用 Intent.FLAG_ACTIVITY_SINGLE_TOP并尝试了launchMode =" singleTask"对于slpashscreen以及主要活动(所有可能性)仍然无法正常工作

4 个答案:

答案 0 :(得分:0)

它的启动画面不是slpashscreen我认为.....这里是一个解决方案调用主要活动的obj到启动画面和添加启动画面到主方法....使用带有启动画面的线程.... / p>

答案 1 :(得分:0)

为SplashScreen定义一个标志(IS_SPLASHSCREEN_LAUNCHED默认值为false),并将其保存在sharedpreference或sqlite数据库中。

使用MainActivty的oncreate()检查标志(IS_SPLASHSCREEN_LAUNCHED),如果是false则启动SplashScreen并将标志设置为true并保存,在fev秒完成启动画面后。 如果确实如此,请不要启动Splashscreen。

注意:您应该通过在SplashScreen Activity中调用SplashScreen.this.finish()来完成启动画面。

答案 2 :(得分:0)

以下是解决方案:

这是

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();
    }
}

在主要活动中,我在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;
    } 
}

我还制作了MainAppActivity LAUNCHER Activity,然后打开Splashscreen活动(如果没有选择)。

</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>

答案 3 :(得分:0)

我建议编写单独的类来保存和检索这样的数据:

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
        }

    }