我正在尝试构建一个OnboardingActivity并且我已经实现了它但是现在我想它应该对新用户可见或者每个用户一次,我尝试了共享首选项但是没有效果。 我使用Splash Screen作为Launcher Activity和MainActivty& onboardingActivity是下一个意图。
SplashActivity
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
getSupportActionBar().hide();
Thread myThread = new Thread() {
@Override
public void run() {
try {
sleep(3000);
Intent intent = new Intent(getApplicationContext(), OnBoardActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
OnBoardActivity
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import com.hololo.tutorial.library.Step;
import com.hololo.tutorial.library.TutorialActivity;
public class OnBoardActivity extends TutorialActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("my_preferences",
MODE_PRIVATE);
// Check if onboarding_complete is false
if(!preferences.getBoolean("onboarding_complete",false)) {
// Start the onboarding Activity
Intent onboarding = new Intent(this, OnBoardActivity.class);
startActivity(onboarding);
// Close the main Activity
finish();
return;
}
//slide1
addFragment(new Step.Builder().setTitle("WELCOME.NAMASTE!!")
.setSummary("It seems like you are a New User!!\nWe welcome you
to GFeed.")
.setBackgroundColor(Color.parseColor("#FFA600")) // int
background color
.setDrawable(R.drawable.gh) // int top drawable
.build());
//slide2
addFragment(new Step.Builder().setTitle("CAREER ORIENTED")
.setSummary("This app includes some awesome career oriented
elements such as Internship,different courses related to CS/IT,career
tips by experts.")
.setBackgroundColor(Color.parseColor("#FFA600")) // int
background color
.setDrawable(R.drawable.human) // int top drawable
.build());
//slide3
addFragment(new Step.Builder().setTitle("COLLEGE ORIENTED")
.setSummary("This app includes some awesome college oriented
elements such as college club news,aktu feed,H.O.D'corner and Gsim")
.setBackgroundColor(Color.parseColor("#FFA600")) // int
background color
.setDrawable(R.drawable.college) // int top drawable
.build());
}
@Override
public void finishTutorial() {
SharedPreferences preferences =
getSharedPreferences("my_preferences", MODE_PRIVATE);
// Set onboarding_complete to true
preferences.edit()
.putBoolean("onboarding_complete",true).apply();
// Launch the main Activity, called MainActivity
Intent main = new Intent(this, MainActivity.class);
startActivity(main);
// Close the OnboardingActivity
finish();
}
}
清单文件
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/vtry"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".developer"
android:screenOrientation="portrait">
</activity>
<activity android:name=".notify"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="NOTIFY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".download"
android:screenOrientation="portrait">
</activity>
<activity android:name=".webvr"
android:screenOrientation="portrait">
/>
<intent-filter>
<action android:name="WEBVR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".gsim"
android:screenOrientation="portrait">
/>
</activity>
<activity android:name=".Internship"
android:screenOrientation="portrait">
/>
</activity>
<activity android:name=".Courses"
android:screenOrientation="portrait">
/>
</activity>
<activity android:name=".Aktu"
android:screenOrientation="portrait">
/>
</activity>
<activity android:name=".Interview"
android:screenOrientation="portrait">
</activity>
<activity android:name=".Reachus"
android:screenOrientation="portrait">
</activity>
<activity android:name=".OnBoardActivity"
android:screenOrientation="portrait">
</activity>
<service
android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/vtry" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
</application>
答案 0 :(得分:1)
在OnboardActivity
中,您正在检查新手是否未完成启动同一活动
逻辑应该是反向的,检查入职是否完成并开始MainActivity
:
if(preferences.getBoolean("onboarding_complete",false)) {
// Start Main Activity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
// Close Onboarding
finish();
return;
}
因此,如果入职未完成,您将继续参加相同的活动
还要检查是否正在调用方法finishTutorial
。