2015-01-07 94 views
0

當我在firstActivity(MainActivity) 2個imageviews使圖像中的第一項活動invisble當我點擊簽到圖像,然後將其移動到SignUp Activity ... 這裏當我註冊圖像點擊然後再它會來MainActivity ..和在這裏,我不得不做出firstName圖像隱形..如何從次活動導航到第一個活動

public class MainActivity extends Activity { 

ImageView firstName,signIn ; 


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

    firstName =(ImageView)findViewById(R.id.imageView1); 
signIn =(ImageView)findViewById(R.id.imageView2); 

    signIn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent intent = new Intent(getApplicationContext(),SignUp.class); 
      startActivity(intent);    

     } 
    });  
    } 



protected void onStart() { 
    super.onStart(); 
    Log.i(TAG, "onStart"); 

    String mm ="5"; 
    Intent i= getIntent(); String s = i.getStringExtra("PrevAct"); 

    if (mm ==s) {    

      firstName.setVisibility(View.GONE); 
    } 
     } 

public class SignUp extends Activity { 


ImageView signUp; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.signup);  

    signUp =(ImageView)findViewById(R.id.imageView3);  
    signUp.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub    

    Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
      intent.putExtra("PrevAct","5"); 
      startActivity(intent); 


     } 
    }); 

}} 

回答

0
  1. 當您嘗試再次啓動在MainActivity活動,確保您重新使用相同的實例,而不是一個新的爲了應用程序更好的性

    Intent intent = new Intent(getApplicationContext(),MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); intent.putExtra(「PrevAct」,「SignUP」); startActivity(intent);

  2. 使用變量來檢查屏幕是否從SignUpActivity呈現。

    Intent i = getIntent(); String s = i.getExtra(「PrevAct」,「NO」);

  3. 根據字符串值,您可以決定顯示/隱藏。

請注意,View.Invisible只隱藏屏幕視圖。但它仍然會被加載並佔用屏幕上的空間。這是一個糟糕的UI實現。

因此改爲使用View.GONE

你的源代碼修改如下

public class MainActivity extends Activity { 

    ImageView firstName,signIn ; 


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

     firstName =(ImageView)findViewById(R.id.imageView1); 
    signIn =(ImageView)findViewById(R.id.imageView2); 

     signIn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       Intent intent = new Intent(getApplicationContext(),SignUp.class); 
       startActivity(intent);    

      } 
     });  
     } 




      protected void onStart() { 
     super.onStart(); 
     firstName = (ImageView)findViewById(R.id.textView1); 

      { 



      Intent i= getIntent(); 
      if(i!=null){ 
      String s = i.getExtra("PrevAct","NO"); 
      if(s.equalsIgnoreCase("SignUP")) 
      firstName.setVisibility(View.GONE); 
      } 
else 
    firstName.setVisibility(View.Visible); 
     } 
    } 

    public class SignUp extends Activity { 

    ImageView signUp; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
      setContentView(R.layout.signup);  

     signUp =(ImageView)findViewById(R.id.imageView3);  
     signUp.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub    

     Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
    intent.putExtra("PrevAct","SignUP"); 
      startActivity(intent); 


      } 
     }); 
    }} 
+0

Intent intent = new Intent(getApplicationContext(),MainActivity.class); \t \t \t \t intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); \t \t \t \t intent.putExtra(「PrevAct」,「5」); \t \t \t \t \t \t \t \t \t \t \t \t startActivity(意向); 保護無效onStart(){ \t \t super.onStart(); \t \t日誌。我(標籤,「onStart」); \t \t \t \t \t 字符串\t毫米= 「5」; \t \t Intent i = getIntent(); String s = i.getStringExtra(「PrevAct」); \t \t \t 如果\t(毫米== S){ \t \t \t \t \t \t \t \t \t loginWith.setVisibility(View.GONE); \t \t \t signIn.setVisibility(View.GONE); \t \t} \t \t \t \t \t} – user1862322

+0

感謝reply..i已經chaged喜歡的但不能getting..in mainactivity我把代碼中的onStop()... – user1862322

0
  1. 通過點擊簽到圖像,使用startActivityForResult開始您的活動簽約。
  2. 在註冊活動時,使用setResult並完成以返回登錄活動。
  3. 當返回到第一個活動時,在onActivityResult中將firstName圖像設置爲不可見或不需要。
相關問題