任务3:理解Task
app1的activity1有两个按钮,一个按钮跳转到Activity2,另外一个按钮跳转到打电话的页面,打电话这个功能不属于app1,我们假设它为app2。
首先默认进入app1的activity1,点击button1后跳转到activity2,再点击activity2的按钮后跳转到activity1,再点击button2跳转到打电话页面,最后依次点返回按钮,大家看Task栈效果。
1.运行效果图
核心代码
主活动MainActivity:
扫描二维码关注公众号,回复:
2770414 查看本文章
package com.example.thirdlab;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
private Button button1;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.but_no1);
button2 = (Button)findViewById(R.id.but_no2);
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent intent1=new Intent(Intent.ACTION_DIAL);
intent1.setData(Uri.parse("tel:10086"));
startActivity(intent1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
主活动布局:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/but_no1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text_but1"
/>
<Button
android:id="@+id/but_no2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/but_no1"
android:text="@string/text_but2"
/>
</RelativeLayout>
第二个活动:
package com.example.thirdlab;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity
{
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button = (Button) findViewById(R.id.but2_no1);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent intent = new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
第二个布局:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >
<Button
android:id="@+id/but2_no1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text_but3"
/>
</RelativeLayout>