2017-06-16 1 views
-1

Ich habe eine übergeordnete Aktivität und eine untergeordnete Aktivität. Die untergeordnete Aktivität hat keine Symbolleiste, daher können Sie nicht zurück navigieren. Sie können nur zur übergeordneten Aktivität zurückkehren, wenn die Zurück-Taste gedrückt wird oder wenn eine bestimmte Taste gedrückt wird (in diesem Fall verwende ich finish()). Ich muss jedoch wissen, ob der Benutzer diese Schaltfläche gedrückt hat, aber wie kann ich der übergeordneten Aktivität über die untergeordnete Aktivität Daten zuweisen?Wie kann ich Daten aus untergeordneten Aktivitäten abrufen, wenn die Zurück-Taste gedrückt wird?

+0

eine Java-Klasse erstellen, die Daten zu halten, als wie MAN IN DER MITTE Kind Tätigkeit handeln wird, dass die Daten in dieser Klasse halten müssen, bevor sie beendet dann Mutter Aktivität lesen Sie diese Klasse, um die Daten lolz zu bekommen –

Antwort

0

Starten Sie Ihr Kind Activity mit startActivityForResult. Dann können Sie ein Ergebnis an den Start (Eltern) zurückkehren Activity

0

In ActivityOne haben wir zwei Elemente 1) Taste 2) Kind Aktivität zu öffnen Textview die Ergebnisse von Kind Aktivität Zwei

In Aktivität binden wir haben zwei Elemente 1) EditText, um Daten einzugeben, die zurück zum Mutter activty 2), um zu senden, müssen die Daten in EditText eingegeben senden

Bitte überprüfen Sie die folgenden Beispielcode für refference

Eltern Aktivität: ActivityOne.java

import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 

    /** 
    * Created by jaffer on 16/6/17. 
    */ 

    public class ActivityOne extends AppCompatActivity implements View.OnClickListener { 

     public static final String DATA = "data"; 
     private static final int REQUEST_CODE = 1001; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_one); 
      initViews(); 
     } 

     private void initViews() { 
      ((Button) findViewById(R.id.btn_one)).setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View view) { 
      Intent i = new Intent(this, ActivityTwo.class); 
      startActivityForResult(i, REQUEST_CODE); 
     } 

     @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) { 
       if (data.getExtras().containsKey(DATA)) { 
        updateUI(data.getExtras().getString(DATA)); 
       } 
      } 
     } 

     private void updateUI(String string) { 
      ((TextView) findViewById(R.id.tv_result)).setText(string); 
     } 
    } 

Eltern Layout activity_one.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/btn_one" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="open child activty"/> 

    <TextView 
     android:id="@+id/tv_result" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp"/> 
</LinearLayout> 

Kinder Aktivität: ActivityTwo.java

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

/** 
* Created by jaffer on 16/6/17. 
*/ 

public class ActivityTwo extends AppCompatActivity implements View.OnClickListener { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_two); 
     initViews(); 
    } 

    private void initViews() { 
     ((Button) findViewById(R.id.btn_send_result)).setOnClickListener(this); 
    } 


    @Override 
    public void onClick(View view) { 
     Intent returnIntent = new Intent(); 
     returnIntent.putExtra(ActivityOne.DATA, ((EditText) findViewById(R.id.et_result)).getText().toString()); 
     setResult(Activity.RESULT_OK, returnIntent); 
     finish(); 
    } 
} 

Kind Layout activity_one.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

    <EditText 
     android:id="@+id/et_result" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dp"/> 

    <Button 
     android:id="@+id/btn_send_result" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Send data"/> 
</LinearLayout> 
Verwandte Themen