2012-07-25 21 views
0

我有三个活动,其中第一个活动是欢迎应用程序页面(SavingsGuiderSplashActivity),它将为要求用户名(SavingsGuiderUserActivity)的第二个活动生成动画。我使用共享首选项来存储用户名。然后点击提交按钮后,它将进入菜单页面(SavingsGuiderMenuActivity)。 ive在第二个页面声明了检索首选方法,所以当用户再次启动应用程序时,如果prefs包含用户名,它将直接进入主菜单活动页面而不是询问用户名的第二个活动。我认为问题更多关于SavingsGuiderSplashActivity和SavingsGuiderUserActivity。尽管我在菜单页上显示名称时没有问题。 (“Hi John”)。我试图产生这个,但不知何故,我第二次启动应用程序,它仍然会进入第二页。有人可以告诉我我的代码有什么问题吗?Android:第二次启动时跳过用户详细信息输入页面

我的动画欢迎页面代码:

public class SavingsGuiderSplashActivity extends SavingsActivity { 


EditText nameEdit; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 
    startAnimating(); 
} 

private void startAnimating() { 
    // Fade in top title 
    TextView img1 = (TextView) findViewById(R.id.TextViewTopTitle); 
    Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in); 
    img1.startAnimation(fade1); 
    // Fade in bottom title after a built-in delay. 
    TextView img2 = (TextView) findViewById(R.id.TextViewBottomTitle); 

    Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2); 
    img2.startAnimation(fade2); 

    // Transition to Main Menu when bottom title finishes animating 
    fade2.setAnimationListener(new AnimationListener() { 
     public void onAnimationEnd(Animation animation) { 

      // The animation has ended, transition to the Main Menu screen    

       startActivity(new Intent(SavingsGuiderSplashActivity.this, SavingsGuiderUserActivity.class)); 
      SavingsGuiderSplashActivity.this.finish(); 
     } 


    @Override 
    public void onAnimationRepeat(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onAnimationStart(Animation animation) { 
     // TODO Auto-generated method stub 

    }}); 

    // Load animations for all views within the TableLayout 

    Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim); 
    LayoutAnimationController controller = new LayoutAnimationController(spinin); 
    TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); 
    for (int i = 0; i < table.getChildCount(); i++) { 
     TableRow row = (TableRow) table.getChildAt(i); 
     row.setLayoutAnimation(controller); 

    } 
} 


     @Override 
     protected void onPause() { 
      super.onPause(); 
      // Stop the animation 
      TextView img1 = (TextView) findViewById(R.id.TextViewTopTitle); 
      img1.clearAnimation(); 
      TextView img2 = (TextView) findViewById(R.id.TextViewBottomTitle); 
      img2.clearAnimation(); 

      TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); 
      for (int i = 0; i < table.getChildCount(); i++) { 
       TableRow row = (TableRow) table.getChildAt(i); 
       row.clearAnimation(); 



     } 
     } 

     @Override 
     protected void onResume() { 
      super.onResume(); 

      // Start animating at the beginning 
      startAnimating(); 

     } 

}

我的用户活动页面在这里:

public class SavingsGuiderUserActivity extends SavingsActivity { 
/** Called when the activity is first created. */ 

    String tag = "SavingsGuiderActivity"; 
    EditText nameEdit; 
    Toast toast;   

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

    Button submitBtn = (Button)findViewById(R.id.btn_submit); 
    nameEdit=(EditText)findViewById(R.id.edit_name); 

    submitBtn.setOnClickListener(new View.OnClickListener() {   
     public void onClick(View v) { 
       String txt = nameEdit.getText().toString();     

       //validate the editText 
       if (!txt.equals("")) {      
       Intent intent = new Intent(SavingsGuiderUserActivity.this, SavingsGuiderMenuActivity.class); 
       Bundle extras = new Bundle(); 
       extras.putString("name",txt); 
       intent.putExtras(extras); 
       saveAsPreferences(); 
       startActivity(intent); 

       } 
       else { 
        Context context = getApplicationContext(); 
        CharSequence text = "Please enter your name!"; 
        int duration = Toast.LENGTH_SHORT; 
        Toast toast = Toast.makeText(context, text, duration); 
        toast.show(); 
       }     

     } 
    }); 

} 

@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    Log.d(tag, "In the onDestroy() event"); 
} 
@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 

    Log.d(tag, "In the onPause() event"); 
} 
@Override 
protected void onRestart() { 
    // TODO Auto-generated method stub 
    super.onRestart(); 
    Log.d(tag, "In the onRestart() event"); 
    retrievePreferences(); 
} 
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume();  
    Log.d(tag, "In the onResume() event"); 
    retrievePreferences(); 
} 
@Override 
protected void onStart() { 
    // TODO Auto-generated method stub 
    super.onStart(); 
    Log.d(tag, "In the onStart() event"); 
    retrievePreferences(); 
} 
@Override 
protected void onStop() { 
    // TODO Auto-generated method stub 
    super.onStop(); 
    Log.d(tag, "In the onStop() event"); 
} 
public void saveAsPreferences(){ 
String nameString = nameEdit.getText().toString(); 
SharedPreferences prefs = getSharedPreferences("preferences", MODE_PRIVATE); 
SharedPreferences.Editor editor = prefs.edit(); 
editor.putString("name", nameString); 
} 
public void retrievePreferences(){ 
SharedPreferences prefs = getSharedPreferences("preferences",MODE_PRIVATE); 
if(prefs.contains("name")){ 
String nameString = prefs.getString("name", ""); 
nameEdit.setText(nameString); 
Intent intent = new Intent(SavingsGuiderUserActivity.this, SavingsGuiderMenuActivity.class); 
Bundle extras = new Bundle(); 
intent.putExtras(extras); 
startActivity(intent); 
} 

} 

}

我的菜单页面在这里:

public class SavingsGuiderMenuActivity extends SavingsActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.menu); 

    Bundle bundle = getIntent().getExtras(); 
    String name= bundle.getString("name"); 

TextView resultView =(TextView)findViewById(R.id.view_Name);

resultView.setText("Welcome " + name); 

    ListView menuList = (ListView) findViewById(R.id.ListView_Menu); 
    String[] items = { getResources().getString(R.string.start), 
      getResources().getString(R.string.about), 
      getResources().getString(R.string.help) }; 
    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items); 
    menuList.setAdapter(adapt); 
    menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) { 
      // Note: if the list was built "by hand" the id could be used. 
      // As-is, though, each item has the same id 
      TextView textView = (TextView) itemClicked; 
      String strText = textView.getText().toString(); 
      if (strText.equalsIgnoreCase(getResources().getString(R.string.start))) { 
       // Launch the Game Activity 
       startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderAppActivity.class)); 
      } else if (strText.equalsIgnoreCase(getResources().getString(R.string.help))) { 
       // Launch the Help Activity 
       startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderHelpActivity.class)); 
      } else if (strText.equalsIgnoreCase(getResources().getString(R.string.about))) { 
       // Launch the Settings Activity 
       startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderAboutActivity.class)); 
      } 
     } 
    }); 
} 

} 

回答

1

尝试在第一页本身(即在SavingsGuiderSplashActivity)检索您的喜好并查看用户名是否存在。使用下面的函数

public boolean usernameExists() 
{ 
SharedPreferences prefs = getSharedPreferences("preferences",MODE_PRIVATE); 
if(prefs.contains("name")) 
{ 
return true; 
} 
else 
{ 
return false; 
} 
} 

等待,我就改变你的SavingsGuiderSplashActivity代码为您

public class SavingsGuiderSplashActivity extends SavingsActivity { 


EditText nameEdit; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.splash); 
startAnimating(); 
} 


private void startAnimating() { 
// Fade in top title 
TextView img1 = (TextView) findViewById(R.id.TextViewTopTitle); 
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in); 
img1.startAnimation(fade1); 
// Fade in bottom title after a built-in delay. 
TextView img2 = (TextView) findViewById(R.id.TextViewBottomTitle); 

Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2); 
img2.startAnimation(fade2); 

// Transition to Main Menu when bottom title finishes animating 
fade2.setAnimationListener(new AnimationListener() { 
    public void onAnimationEnd(Animation animation) { 

     // The animation has ended, transition to the Main Menu screen    

     if(!usernameExists()) 
     { 
      startActivity(new Intent(SavingsGuiderSplashActivity.this, SavingsGuiderUserActivity.class)); 
     } 
      else 
      { 
      startActivity(new Intent(SavingsGuiderSplashActivity.this, SavingsGuiderMenuActivity.class)); 
      } 
     SavingsGuiderSplashActivity.this.finish(); 
    } 

public boolean usernameExists() 
{ 
SharedPreferences prefs = getSharedPreferences("preferences",MODE_PRIVATE); 
if(prefs.contains("name")) 
{ 
return true; 
} 
else 
{ 
return false; 
} 
} 

@Override 
public void onAnimationRepeat(Animation animation) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onAnimationStart(Animation animation) { 
    // TODO Auto-generated method stub 

}}); 

// Load animations for all views within the TableLayout 

Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim); 
LayoutAnimationController controller = new LayoutAnimationController(spinin); 
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); 
for (int i = 0; i < table.getChildCount(); i++) { 
    TableRow row = (TableRow) table.getChildAt(i); 
    row.setLayoutAnimation(controller); 

} 
} 


    @Override 
    protected void onPause() { 
     super.onPause(); 
     // Stop the animation 
     TextView img1 = (TextView) findViewById(R.id.TextViewTopTitle); 
     img1.clearAnimation(); 
     TextView img2 = (TextView) findViewById(R.id.TextViewBottomTitle); 
     img2.clearAnimation(); 

     TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); 
     for (int i = 0; i < table.getChildCount(); i++) { 
      TableRow row = (TableRow) table.getChildAt(i); 
      row.clearAnimation(); 



    } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     // Start animating at the beginning 
     startAnimating(); 

    } 

这应该修复它!

+0

谢谢!我是否需要删除其他活动页面中的任何代码,或只添加上面的代码? – honeybee 2012-07-25 13:37:34

+0

嘿,我试过了,但是它仍然会在第二次启动时提示输入用户名......实际上我第二次启动时,它进入了用户页面n跳过了启动动画页面。 – honeybee 2012-07-25 13:48:44

+0

这真的很奇怪。您需要删除用户页面中的retrievePreferences。你只需要检查用户名是否存在于第一页,如果它存在,你可以跳到第三页。这就是这段代码的用法(!usernameExists()) { startActivity(new Intent(SavingsGuiderSplashActivity.this,SavingsGuiderUserActivity.class)); } else startActivity(new Intent(SavingsGuiderSplashActivity.this,SavingsGuiderMenuActivity.class)); } – 2012-07-26 07:00:50

0

在,如果你有一个用户名检索偏好的底部,可以尝试:

this.finish(); 
+0

你的意思是在用户活动页面,在检索首选项方法之后startActivity(意向);,我把this.finish( ); ? – honeybee 2012-07-25 13:29:57

+0

好的ive添加了它。第一次启动应用程序时,它会提示输入我的名字。在我输入之后,它会引导我进入菜单页面。然后当我按下硬件后退按钮一路到Android主页并重新启动我的应用程序,它仍然提示我为我的名字.. – honeybee 2012-07-25 13:32:54

0

如果我理解正确的话,你要直接从SavingsGuiderSplashActivity移动第二次 - >SavingsGuiderMenuActivity,有跳过SavingsGuiderUserActivity。如果这是真的,那么在你的SavingsGuiderSplashActivity的 onAnimationEnd方法,你需要检查,如果优先值设置,并根据您需要推出相应的活动,

+0

是的,你是正确的。但我如何检查他们?我是否需要重新声明共享并在splash活动中重新检索,然后执行if else语句? – honeybee 2012-07-25 13:28:10

0

所以这就是我的老师教我让爱应用程序运行!

用户活动页面

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.user);   

    Button submitBtn = (Button)findViewById(R.id.btn_submit); 
    nameEdit=(EditText)findViewById(R.id.edit_name); 

    submitBtn.setOnClickListener(new View.OnClickListener() {   
     public void onClick(View v) { 
       String txt = nameEdit.getText().toString();     

       //validate the editText 
       if (!txt.equals("")) {      
       Intent intent = new Intent(SavingsGuiderUserActivity.this, SavingsGuiderMenuActivity.class); 
       Bundle extras = new Bundle(); 
       extras.putString("name",txt); 
       intent.putExtras(extras); 
       saveAsPreferences(); 
       startActivity(intent); 

       } 
       else { 
        Context context = getApplicationContext(); 
        CharSequence text = "Please enter your name!"; 
        int duration = Toast.LENGTH_SHORT; 
        Toast toast = Toast.makeText(context, text, duration); 
        toast.show(); 
       }     

     } 
    }); 

} 

@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    Log.d(tag, "In the onDestroy() event"); 
} 
@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 

    Log.d(tag, "In the onPause() event"); 
} 
@Override 
protected void onRestart() { 
    // TODO Auto-generated method stub 
    super.onRestart(); 
    Log.d(tag, "In the onRestart() event"); 
} 
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume();  
    Log.d(tag, "In the onResume() event"); 
} 
@Override 
protected void onStart() { 
    // TODO Auto-generated method stub 
    super.onStart(); 
    Log.d(tag, "In the onStart() event"); 
} 
@Override 
protected void onStop() { 
    // TODO Auto-generated method stub 
    super.onStop(); 
    Log.d(tag, "In the onStop() event"); 
} 
public void saveAsPreferences(){ 
String nameString = nameEdit.getText().toString(); 
SharedPreferences prefs = getSharedPreferences("preferences", MODE_PRIVATE); 
SharedPreferences.Editor editor = prefs.edit(); 
editor.clear(); 
editor.putString("name", nameString); 
editor.commit(); 
} 
} 

飞溅活动:

public void onAnimationEnd(Animation animation) { 

      // The animation has ended, transition to the Main Menu screen 
      if(!usernameExists()) 
      { 
       startActivity(new Intent(SavingsGuiderSplashActivity.this, SavingsGuiderUserActivity.class)); 
      } 
      else 
      { 
       Intent intent = new Intent(SavingsGuiderSplashActivity.this, SavingsGuiderMenuActivity.class); 
       Bundle extras = new Bundle(); 
       extras.putString("name",strName); 
       intent.putExtras(extras); 
       startActivity(intent);     
      } 
      SavingsGuiderSplashActivity.this.finish(); 
     } 

    public boolean usernameExists() 
    { 
    SharedPreferences prefs = getSharedPreferences("preferences",MODE_PRIVATE); 
    if(prefs.contains("name")) 
    { 
     strName = prefs.getString("name", null); 
    return true; 
    } 
    else 
    { 
    return false; 
    } 
    } 
+0

接受我的答案然后稍微修改并且不接受我的答案并将其作为自己的答案发布的意义何在?你可能只是要求我用这4行来编辑我的代码。 Bundle extras = new Bundle(); extras.putString(“name”,strName); intent.putExtras(extras); startActivity(intent); – 2012-07-28 09:47:59

+0

对于人们在回答您的问题时所付出的努力表示尊重。 – 2012-07-28 09:49:33

+0

我接受了!蜱是绿色的!对不起,如果我冒犯了你,因为我刚刚进入这个网站!最初,我勾选了人们给我的所有答案,然后我发现我只能勾选出“未接通知”。然后我马上勾选你的答案,因为没有你给我的代码,我会卡住到现在。我发誓一直都有绿剔。 – honeybee 2012-07-28 11:14:27