Android - Neue Activity
Neue Activity mit Datenrückgabe
Stichworte
Beispiel 1
Neue Activity wird mit Klick gestartet, je nachdem welcher Knopf gedrückt wurde werden andere Daten übergeben MainActivity.java
...
@Override
public void onClick(View v) {
// v holds the clicked view ie a button
// Get a Intent to connect the actual context (this) to Second Activity
Intent i = new Intent(this,SecondActivity.class);
// Switch to find out which Button has been clicked
switch (v.getId()) {
case R.id.button1:
/* Use intent to transport data.
In this case we use a data package named myData
it holds only a string */
i.putExtra("myData","hello from button1");
/* special startActivity type because we're awaiting data back -
this way we can give the new activity a back channel */
startActivityForResult(i,1);
break;
case R.id.button2:
i.putExtra("myData","hello from button2");
startActivityForResult(i,1);
break;
default:
break;
}
}
// Cause we used startActivityForResult we get a onActivityResult Event if the NumbersActivity sends us data back
// Here we can catch it and send it i.e. to a textField
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// now we can use the returned data
if(data.getExtras().containsKey("backInfo")){
myTextField.setText(data.getStringExtra("backInfo"));
}
}
SecondActivity.java
@Override
public void onClick(View v) {
// get Text i.e. from a input field
String s = input.getText().toString();
String s2 = "Gruss zurück";
// Grab the intent which has called the activity
Intent i = getIntent();
// Get the Extra Data from the intent which we stored in myData (Imagine a basket labelled myData)
String msg = i.getStringExtra("myData");
if(msg.contentEquals("hello from button1")){
// OK got it - should send back width information - so put it in a package
i.putExtra("backInfo",s);
// and send it back
setResult(RESULT_OK, i);
// and finish this activity
finish();
}
if(msg.contentEquals("hello from button 2")){
i.putExtra("backInfo",s2);
setResult(RESULT_OK, i);
finish();
}
}
Neue Activity starten
Z.B. über Button Klick
Du brauchst:
- Neue Activity
- Evtl. neues Layout anlegen
- Neue Java Klasse für Activity anlegen
- Neue Activity im Manifest eintragen
- Button in der Haupt Activity
- onClick Event Handler
- Intent als Verbindung
- Ressourcen wie Strings etc.
Neues Layout für die Activity
Layout Ordner > Rechtsklick > New > Android Layout File > Name in Lowercase(z.b. page_2)
<?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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/page2_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Neue Activity Klasse erzeugen
Package > Rechtsklick > New > Class > Name mit FirstLetterUppercase (z.B. Page2Activity)
- extends Activity
- Activity importieren
- mind. onCreate(bundle) -> am Besten über Rechtsklick > Source > Override
- setcontentView(R.layout.id)
page2Activity.class
package com.example.buttonstartsactivity;
import android.app.Activity;
import android.os.Bundle;
public class Page2Activity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
}
}
Manifest Eintrag
// ...
<activity
android:name="com.example.buttonstartsactivity.Page2Activity"
android:label="@string/app_name"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.PAGE2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
// ...
Hinweis oft kann man sich den intent-filter sparen. Man braucht ihn wenn man von anderen apps auf die activity zugreifen will. Läßt man ihn weg kann man einfach die activity mit.
Button in Hauptansicht erstellen
z.B.
main_activtiy.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/button" />
</RelativeLayout>
Button verbinden
MainActivity.java
package de.webmynet.buttonstartsactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//btw v is the view where the button is pressed - you can use it to retrieve data
startActivity(new Intent("de.webmynet.buttonstartsactivity.PAGE2"));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Variante onClick Event im Layout File
Anstatt der onClick Funktion kann man auch eine Funktion im Layout definieren. Dann kann man in der .java Datei einfach die Funktion implementieren. activity_main.xml
... <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:onClick="buttonPressed" /> ...
MainActivity.java
/** Called when the user clicks the send button **/
public void buttonPressed(View view) {
Intent intent = new Intent(this, Page2Activity.class);
EditText editText = (EditText) findViewById(R.id.button);
startActivity(intent);
}