2013-03-29 26 views
13

Tôi đang làm việc trên một ứng dụng Android. Tôi muốn thay đổi Bắt đầu hoạt động động. tôi có nghĩa là khi người dùng bắt đầu ứng dụng lần đầu tiên sau đó bắt đầu hoạt động sẽ khác nhau và khi bắt đầu thay đổi hoạt động lần thứ hai. Điều này sẽ bỏ qua hai hoạt động đầu tiên và chuyển sang hoạt động thứ ba.Làm thế nào để thay đổi hoạt động bắt đầu động?

Trả lời

0

Sử dụng tùy chọn để lưu trữ các giá trị (Điều kiện) mà bạn muốn có. sau đó theo đó thay đổi startActivity.

1

bạn có thể sử dụng SharedPreference theo Yêu cầu của bạn.

bạn có thể lưu trữ và lấy giá trị từ này Link

Bên trong mỗi phương pháp Oncreate() của Hoạt động của bạn, bạn có thể Check for Value SharedPreference và bắt đầu hoạt động của bạn ở đó.

Hy vọng nó sẽ giúp bạn.

0

Sử dụng sharedpreference để lần đầu tiên họ đăng nhập hay không

if (!checkNameInfo()) { 
//first time 
        FirstActivity(); 
       } else { 
//second time 
        Intent i = new Intent(first.this, second.class); 
        startActivity(i); 
        finish(); 
       } 

Kiểm tra giá trị

private final boolean checkNameInfo() { 
     boolean role = mPreferences.contains("Name"); 
     if (role) { 
      return true; 
     } 
     return false; 
    } 

TRÊN firstActivity

SharedPreferences.Editor editor = mPreferences.edit(); 
       editor.putString("Name", edt.getText().toString()); 
editor.commit(); 
Intent i = new Intent(first.this, second.class); 
         startActivity(i); 
0

Đây là những gì tôi làm khi một người dùng đã lựa chọn một Nhớ thông tin đăng nhập của tôi trên Màn hình Đăng nhập.

Để tiết kiệm giá trị cho SharedPreference file:

Bạn sẽ cần phải làm một cái gì đó như thế này một lần trong Activity đầu tiên và một lần vào thứ hai Activity

sharedPrefs = getApplicationContext().getSharedPreferences(PRIVATE_PREF, Context.MODE_PRIVATE); 

// EDITOR INSTANCE TO SAVE THE NAG SETTING 
editor = sharedPrefs.edit(); 

// GET THE NAG SETTING CHECKBOX 
if (chkbxNagSetting.isChecked()) { 

    editor.putBoolean(NAG_SETTING, true); 
} else { 
    editor.putBoolean(NAG_SETTING, false); 
} 

editor.commit(); 

Để lấy giá trị từ tập tin SharedPreference :

boolean blNagSetting = sharedPrefs.getBoolean(NAG_SETTING, false); 

if (blNagSetting == true) { 
    Intent startMainPage = new Intent(SignIn.this, SplashScreen.class); 
    startMainPage.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(startMainPage); 
    finish(); 
} 

Và đây là các biến/trường hợp cần thiết toàn cầu được sử dụng trong Activity:

SharedPreferences sharedPrefs; 
Editor editor; 
private static final String PRIVATE_PREF = "CHANGE_TO_SOME_FILE_NAME"; 
private static final String NAG_SETTING = "nag_setting"; 

Bạn sẽ phải sửa đổi mã một chút để tính toán bỏ qua 2 Activities.

Hãy cho tôi biết nếu bạn cần bất kỳ trợ giúp nào.

0

Bất kể lần đầu tiên mở ứng dụng của bạn trong Hoạt động chính là gì. Trong khi đó, hãy sử dụng SharedPreference để lưu dữ liệu về số lần bạn đã tải ứng dụng.

Bạn sẽ phải làm một cái gì đó như sau trong bạn

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    String dataAvailable; 
    SharedPreferences prefs = getSharedPreferences("countPref", Context.MODE_PRIVATE); 
    dataAvailable = prefs.getString("dataAvailable", null); 

    //checking whether launching for the first time. 
    if(dataAvailable!=null){ 
     int appLoadedCount = prefs.getInt("appLoadedCount", -1); 
     appLoadedCount++; 
     prefs.edit().putInt("appLoadedCount", appLoadedCount).commit(); 

     // Check how many times loaded 
     if(appLoadedCount==0){ 
      Intent firstAct = new Intent(MainActivity.this, FirstActivity.class); 
      startActivity(firstAct); 
     } 
     else if(appLoadedCount==1){ 
      Intent scndAct = new Intent(MainActivity.this, ScndActivity.class); 
      startActivity(scndAct); 
     } 
     else if(appLoadedCount==2){ 
      Intent thirAct = new Intent(MainActivity.this, ThirdActivity.class); 
      startActivity(thirAct); 
     } 
     else{ 
      Intent thirAct = new Intent(MainActivity.this, ThirdActivity.class); 
      startActivity(thirAct); 
     } 

     Log.v("avilable", dataAvailable); 
     Log.v("avilable", String.valueOf(appLoadedCount)); 
    } 
    else{ 
     //loading first time 
     prefs.edit().putString("dataAvailable", "yeap").commit(); 
     //setting the count to 1 as loaded for the firs time 
     prefs.edit().putInt("appLoadedCount", 0).commit(); 
     Log.v("Not avilable", "Loaded first time"); 
    } 


} 
28

Bạn không thể thay đổi hoạt động đầu tiên động, nhưng bạn có thể tạo ra một hoạt động minh bạch như thế này:

<activity 
    android:name=".ActivityLauncher" 
    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

và chọn hoạt động tiếp theo trong phương thức onCreate:

if (logged()) { 
    intent = new Intent(this,MainActivity.class); 
} else { 
    intent = new Intent(this,SignInActivity.class); 
} 
startActivity(intent); 
finish(); 
+0

Nó hoạt động tốt. –

+0

có cần bố cục không ?? –

0

Nó không cần thiết ary rằng Hoạt động phải có tệp bố cục. Bạn có thể kiểm tra điều kiện trong hoạt động trình khởi chạy và chuyển hướng đến hoạt động khác dựa trên điều kiện. (Tuy nhiên, quá trình chuyển đổi từ hoạt động của trình khởi chạy sang hoạt động điều kiện sẽ không hiển thị).

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
Intent intent; 
if (condition) { 
    intent = new Intent(this, FirstClass.class); 
} else { 
    intent = new Intent(this, SecondClass.class); 
} 
startActivity(intent); 
finish(); 
// note we never called setContentView() 
} 

Hoạt động khác (Firstclass/SecondClass):

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
} 
Các vấn đề liên quan