2013-03-29 21 views
13

मैं एक एंड्रॉइड ऐप पर काम कर रहा हूं। मैं गतिशील रूप से प्रारंभ गतिविधि को बदलना चाहता हूं। मेरा मतलब है कि जब उपयोगकर्ता पहली बार ऐप शुरू करता है तो गतिविधि शुरू हो जाएगी और दूसरी बार शुरू होने पर गतिविधि में बदलाव शुरू हो जाएगा। यह पहली दो गतिविधि छोड़ देगा और तीसरी गतिविधि में चलेगा। मैं इसे कैसे प्राप्त कर सकता हूं।गतिशील गतिविधि को गतिशील रूप से कैसे बदलें?

उत्तर

0

उन मानों (शर्तों) को संग्रहीत करने के लिए वरीयताओं का उपयोग करें जिन्हें आप चाहते हैं। उसके बाद उस परिवर्तन के अनुसार प्रारंभ गतिविधि।

1

आप अपनी आवश्यकता के अनुसार SharedPreference का उपयोग कर सकते हैं।

आप स्टोर और अपने गतिविधि के प्रत्येक Oncreate() विधि आप SharedPreference मूल्य के लिए जाँच करें और वहाँ अपनी गतिविधि शुरू कर सकते हैं अंदर यह Link

से मान प्राप्त कर सकते हैं।

आशा है कि यह आपकी मदद करेगा।

0

पहली बार के लिए sharedpreference उपयोग वे लॉग इन किया है या नहीं

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

चेक मूल्य

private final boolean checkNameInfo() { 
     boolean role = mPreferences.contains("Name"); 
     if (role) { 
      return true; 
     } 
     return false; 
    } 
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

यह मैं क्या कर तो किसी उपयोगकर्ता द्वारा चयनित किया गया है मुझे लॉग इन स्क्रीन पर चेक बॉक्स याद रखें।

SharedPreference फाइल करने के लिए मूल्य सहेजने के लिए:

आप SharedPreference फ़ाइल से मान प्राप्त करने के लिए दूसरा 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(); 

में पहली Activity में एक बार कुछ इस तरह करते हैं और एक बार करने की आवश्यकता होगी :

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

और येमें आवश्यक वैश्विक चर/उदाहरण हैं:

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

आप 2 Activities लंघन के लिए खाते थोड़ा कोड को संशोधित करना होगा।

मुझे किसी भी मदद की ज़रूरत है तो मुझे बताएं।

0

इससे कोई फर्क नहीं पड़ता कि मुख्य गतिविधि में आपके ऐप को पहले खोलें। इस बीच डेटा को सहेजने के लिए SharedPreference का उपयोग करें कि आपने कितनी बार ऐप लोड किया है।

आप वाला में नीचे के रूप में कुछ करने के लिए अपने

@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

आप पहली गतिविधि गतिशील रूप से परिवर्तित नहीं कर सकते, लेकिन आप इस तरह एक पारदर्शी गतिविधि बना सकते हैं:

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

और अगले गतिविधि का चयन onCreate विधि में:

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

यह अच्छी तरह से काम करता है। –

+0

क्या इसे लेआउट की आवश्यकता है ?? –

0

यह आवश्यक नहीं है आरी कि एक गतिविधि में लेआउट फ़ाइल होनी चाहिए। आप अपनी लॉन्चर गतिविधि में एक शर्त जांच कर सकते हैं और शर्त के आधार पर अन्य गतिविधि पर रीडायरेक्ट कर सकते हैं। (लॉन्चर गतिविधि से स्थिति गतिविधि में संक्रमण हालांकि दिखाई नहीं देगा)।

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

अन्य गतिविधि (Firstclass/SecondClass):

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
} 
संबंधित मुद्दे