2013-03-07 84 views
2

我在活動A中,現在我導航到活動B.在完成活動B中的工作後,我切換回活動A(在按鈕的onclick()事件中調用finish();函數)。完成();函數通過恢復活動A將我帶回活動A.如何從活動B導航到A以及數據。如何將數據從一個活動導航到另一個活動?

這裏是代碼: - 活動答: -

NewMain.java

public void onRestoreInstanceState(Bundle savedInstanceState) { 
     // Always call the superclass so it can restore the view hierarchy 
     super.onRestoreInstanceState(savedInstanceState); 

     // Restore state members from saved instance 

     alarmname1 = savedInstanceState.getString("EXTRA_MESSAGE"); 
     range1 = savedInstanceState.getInt("EXTRA_MESSAGE_RANGE", 100); 
     //Toast.makeText(NewMain.this, longi1, 2000).show(); 




    } 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == 1) { 

      if(resultCode == RESULT_OK){ 

       /*Bundle bu_lati = getIntent().getExtras(); 
        lati1= Double.parseDouble(bu_lati.getString("EXTRA_MESSAGE_LATITUDE")); 
        Bundle bu_longi = getIntent().getExtras(); 
        longi1=Double.parseDouble(bu_longi.getString("EXTRA_MESSAGE_LONGITUDE")); 
        */ 

       Bundle lati1=data.getBundleExtra("EXTRA_MESSAGE_LATITUDE"); 
       Bundle longi1=data.getBundleExtra("EXTRA_MESSAGE_LONGITUDE"); 
      } 
      if (resultCode == RESULT_CANCELED) {  
       //Write your code on no result return 
      } 
      } 
     } 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.newgui1); 

     alarmname=(EditText) findViewById(R.id.editText1); 
     range=(EditText) findViewById(R.id.editText2); 
     Button button = (Button) findViewById(R.id.button2); 
     im1=(ImageButton) findViewById(R.id.imageButton1); 

     Button lnl = (Button) findViewById(R.id.button3); 

     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       //alarmname 

       alarmname1=alarmname.getText().toString(); 
       //range 


       range1=Integer.parseInt(range.getText().toString()); 

       Intent myIntent = new Intent(NewMain.this, NewMain2.class); 
       myIntent.putExtra("EXTRA_MESSAGE", alarmname1); 
       myIntent.putExtra("EXTRA_MESSAGE_RANGE", range1); 
       myIntent.putExtra("EXTRA_MESSAGE_LATITUDE1", lati1); 
       myIntent.putExtra("EXTRA_MESSAGE_LONGITUDE1", longi1); 
       NewMain.this.startActivity(myIntent); 

      } 
     }); 

     lnl.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       Toast.makeText(NewMain.this, longi1, 2000).show(); 



      } 
     }); 
     im1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       Intent myIntent = new Intent(NewMain.this, MapViewDemo.class); 
       NewMain.this.startActivity(myIntent); 

      } 
     }); 
     } 

    } 

活動B: -

MapViewDemo.java 



public boolean onTouchEvent(MotionEvent event, MapView mapView) 
      { 
       //---when user lifts his finger--- 
       if (event.getAction() == 1) {     
        GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY()); 
        lati=p.getLatitudeE6()/1E6; 
        longi=p.getLongitudeE6() /1E6; 
         Toast.makeText(getBaseContext(), lati+ "," +longi, 
          Toast.LENGTH_SHORT).show(); 


       }        
       return false; 
      }   

     } 

      Button buttondone = (Button) findViewById(R.id.btndone); 
      buttondone.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 



        Intent myIntent1 = new Intent(); 

        myIntent1.putExtra("EXTRA_MESSAGE_LATITUDE", lati); 
        myIntent1.putExtra("EXTRA_MESSAGE_LONGITUDE", longi); 

        setResult(NewMain.RESULT_OK, myIntent1); 
        finish(); 




       } 
      }); 

    } 

回答

3

啓動B活性喜歡,

Intent intent = new Intent(ActivityA.this, ActivityB.class); 
    startActivityForResult(intent, 1); 

之前完成B活性的setResult( )like,

Intent i =new Intent(); 
    i.putExtra("yourdata", "abc"); 
    setResult(2, i); 
    finish(); 

您的A活動

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode==2){ 
     String s = data.getStringExtra("yourdata"); 
    } 
} 

這樣您就可以將數據從子活動傳遞給父活動。從您的活動newMain

1

做到這一點,最好的辦法是使用模式startActivityForResult需要一個意圖和一個請求代碼作爲參數。

與startActivity相同,但您可以在活動B完成時等待結果。

在完成活動B之前,您必須在活動B上使用setResult,然後完成()。 setResult需要2個參數:一個意圖(你想傳回給A的數據)和一個resultCode(ok,錯誤代碼)。

對活性產生可以等待並抓住結果重寫方法onActivityResult

這裏有一個很好的教程:Getting a Result from an Activity

0

通requestcode和使用的startActivityForResult代替
NewMain.this.startActivity(myIntent)

startActivityForResult(intent, 10); 

現在使你的(requestCode == 10)在你的if條件。

希望它能工作

0

使用此代碼。
要啓動第二次活動:

Intent myIntent = new Intent(NewMain.this, MapViewDemo.class); 
startActivityForResult(launchNewIntent, 2); 

在活動一

int result1 ; 
int result2 ; 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 2) { 
     if (resultCode == RESULT_OK) { 
      result1=data.getExtras().getInt("EXTRA_MESSAGE_LATITUDE"); 
      result2 =data.getExtras().getInt("EXTRA_MESSAGE_LONGITUDE"); 
     } 
    } 
} 

在活動B

 Intent myIntent1 = new Intent(); 
    myIntent1.putExtra("EXTRA_MESSAGE_LATITUDE", lati); 
    myIntent1.putExtra("EXTRA_MESSAGE_LONGITUDE", longi); 
    setResult(Activity.RESULT_OK, resultData); 
    finish(); 
0

link你就會明白的整個概念..

相關問題