android 重新启动应用程序,通过单击应用程序图标打开Android应用程序时重新启动...

我是

Android开发世界的新手,我已经建立了一个简单的“Hello World”应用程序.首先,活动请求一个文本.当单击“Go”按钮时,应用程序将启动显示输入文本的第二个活动.

如果我单击HOME按钮,然后单击应用程序图标,该应用程序将再次启动第一个活动,但是如果我按住主屏幕按钮并单击“最近的应用程序”栏中的图标,它会恢复我离开的应用程序.

如何避免这种情况?

即使点击启动器图标,我也需要我的应用程序恢复.

MainActivity.java,

package com.example.myfirstandroidapp;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.widget.EditText;

public class MainActivity extends Activity {

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@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;

}

/** Called when the user clicks the Send button */

public void sendMessage(View view){

// Do something in response to button

Intent intent = new Intent(this,DisplayMessageActivity.class);

EditText editText = (EditText) findViewById(R.id.txtName);

String message = editText.getText().toString();

intent.putExtra(EXTRA_MESSAGE,message);

startActivity(intent);

}

}

DisplayActivity.java,

package com.example.myfirstandroidapp;

import android.annotation.TargetApi;

import android.app.Activity;

import android.content.Intent;

import android.os.Build;

import android.os.Bundle;

import android.support.v4.app.NavUtils;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Get the message from the intent

Intent intent = getIntent();

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

// Create the text view

TextView textView = new TextView(this);

textView.setTextSize(40);

textView.setText(message);

// Set the text view as the activity layout

setContentView(textView);

// Show the Up button in the action bar.

setupActionBar();

}

/**

* Set up the {@link android.app.ActionBar},if the API is available.

*/

@TargetApi(Build.VERSION_CODES.HONEYCOMB)

private void setupActionBar() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

getActionBar().setDisplayHomeAsUpEnabled(true);

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.display_message,menu);

return true;

}

@Override

public void onDestroy(){

super.onDestroy();

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case android.R.id.home:

// This ID represents the Home or Up button. In the case of this

// activity,the Up button is shown. Use NavUtils to allow users

// to navigate up one level in the application structure. For

// more details,see the Navigation pattern on Android Design:

//

// http://developer.android.com/design/patterns/navigation.html#up-vs-back

//

NavUtils.navigateUpFromSaMetask(this);

return true;

}

return super.onOptionsItemSelected(item);

}

}

activity_main.xml中,

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" >

android:id="@+id/txtName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="70dp"

android:ems="10" >

android:id="@+id/btnGo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/txtName"

android:layout_alignParentRight="true"

android:onClick="sendMessage"

android:text="Go!" />

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/txtName"

android:layout_alignParentTop="true"

android:layout_marginTop="18dp"

android:text="Please input your name:"

android:textAppearance="?android:attr/textAppearanceMedium" />

activity_display_message.xml,

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=".DisplayMessageActivity" >

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

AndroidManifest.xml中,

package="com.example.myfirstandroidapp"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="9"

android:targetSdkVersion="10" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.example.myfirstandroidapp.MainActivity"

android:label="@string/app_name" >

android:name="com.example.myfirstandroidapp.DisplayMessageActivity"

android:label="@string/title_activity_display_message"

android:parentActivityName="com.example.myfirstandroidapp.MainActivity" >

android:name="android.support.PARENT_ACTIVITY"

android:value="com.example.myfirstandroidapp.MainActivity" />

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/359681.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

继承,is,as,多态

继承中的构造方法:1、创建子类对象时,一定会先创建父类对象2、如果调用的子类构造方法没有使用base,就会自动调用父类无参的构造方法, 如果父类没有无参的构造方法就会报错3、如果调用的子类构造方法使用了base,就会…

太古鸿蒙诀正式版v1.07,百变队长安崎:台上小辣椒,台下情歌王

在舞台上魅力四射的THE9队长安崎,在台下却是喜欢唱情歌的软萌girl。这样的安崎你爱了么?反差萌王者安崎作为队长,在舞台上的表现一直都是“炸裂”、“辣”、“性感”、“野性”,而舞台之下的安崎则一直呈现的是可爱、甜美&#xf…

erlang 架构原理_Erlang与Java内存架构

erlang 架构原理我读了一篇关于Erlang VM的内存管理策略的非常非常有趣的文章。 它是Jesper Wilhelmsson撰写的论文 ,我认为讨论Erlang的内存设置和Oracle的Java VM之间的差异可能会很好。 作为对从未听说过Erlang的人们的一个简短的介绍; 它是一种功能语言&#xf…

ASP.NET MVC IOC 之AutoFac攻略

一、为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用AutoFac的貌似更为普遍,于是捯饬了两天,发现这个东东确实是个高大上的IOC容器~ Autofac是.NET领域最为流行的IOC框架之一,传说是速度最快…

android对话框跳转页面,android应用Dialog跳转到Activity

Java基础-四要素之一《多态》什么是多态 指允许不同类的对象对同一消息做出响应.即同一消息可以根据发送对象的不同而采用多种不同的行为方式.(发送消息就是函数调用) 多态是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的 ...java进制转换器 图形用户界面 十进…

apache cxf_Apache CXF负载平衡和故障转移

apache cxf不久前,我们已经面临了基于Apache CXF的负载平衡Web服务客户端的需求。 此外,当某些服务器关闭时,客户端应自动进行故障转移。 更糟糕的是,服务器目标地址列表要从外部服务获取并在运行时更新。 最终,我们最…

HDU 1874 最直接的最短路径问题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid1874 Problem Description某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择&#xf…

晶体管

晶体管-沟道 场效应-电容性的控制沟道 势效应-直接控制沟道 平带电压 由于半导体和金属的功函数的不同,导致半导体表面层并不属于平带状态,为了恢复平带状态所加的电压为平带电压,Vfb 转载于:https://www.cnblogs.com/rice808/p/3874597.html…

领域驱动设计模式设计与实践_在域驱动设计中使用状态模式

领域驱动设计模式设计与实践域驱动设计(DDD)是一种开发软件的方法,其中,通过将实现与核心业务概念的不断发展的模型相联系,解决了问题的复杂性。 该术语是由Eric Evans创造的,并且有一个DDD专用站点可以促进…

html 英文文字纵向排列,CSS几种简单方法实现文字竖向排版

1.一个句子的竖向排列如图:1.2. test.one {width: 20px;margin: 0 auto;line-height: 24px;font-size: 20px;}.two {width: 15px;margin: 0 auto;line-height: 24px;font-size: 20px;word-wrap: break-word;/*英文的时候需要加上这句,自动换行*/}我是竖列…

jstree 节点拖拽保存数据库

需要jstree具有拖拽功能需要在加载jstree时添加dnd插件,具体看代码: $(**).jstree({//plugins-各种jstree的插件引入,展示树的多样性 plugins : [ "dnd", "types", "wholerow" ], core : {"check_callbac…

html js utf8编码转换,js 编码转换 gb2312 和 utf8 互转的2种方法

方法一:function gb2utf8(data){var glbEncode [];gb2utf8_data data;execScript("gb2utf8_data MidB(gb2utf8_data, 1)", "VBScript");var tescape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/…

向其他进程注入代码的三种方法

如何向其他线程的地址空间中注入代码并在这个线程的上下文中执行之? 目录:●导言●Windows 钩子(Hooks)●CreateRemoteThread 和LoadLibrary 技术○进程间通讯●CreateRemoteThread 和 WriteProcessmemory 技术○如何使用该技术子…

自动添加html结束标志,HTML:包含或排除可选的结束标记?

MYYA我在这里添加一些链接来帮助您了解HTML的历史,以便您了解各种矛盾。这不是你的问题的答案,但在阅读这些各种摘要后你会知道更多。我们是怎么来到这里的? - 潜入HTML5网络历史HTML简史HTML的历史 - HTML WG WikiDive Into HTML5的一些摘录…

JAR清单类路径不仅适用于Java Application Launcher

自从我开始学习Java以来​​,我几乎已经知道, 清单文件中的Class-Path标头字段为可执行JAR (具有由另一个称为Main-Class清单指定应用程序起点的 JAR)指定相对运行时类路径。 一个同事最近碰到一个让我感到惊讶,因为它…

[原创]ActionScript3游戏中的图像编程(连载五)

总目录:http://www.cnblogs.com/iloveas/p/3879125.html 1.1.2 Flash中的ARGB模式与不透明度的关系 ARGB是Flash,svg等矢量处理软件特有的一种色彩模式,事实上我觉得它有点扯淡,A(alpha)不应该作为一个通道…

通过url,获取html内容,并解析,如何使用 JavaScript 解析 URL

在 Web 开发中,有许多情况需要解析 URL,这篇主要学习如何使用 URL 对象实现这一点。开始创建一个以下内容的 HTML 文件,并在浏览器中打开。JavaScript URL parsing// 激动人心的代码即将写在这里如果你想尝试本文中的任何内容,可以…

define 汉字 error C2001: newline in constant

这个问题真的很让我头大,搜了很多办法都不行,问题是我之前也遇到过,但是编码转为utf-8 unsignature就行了,这次把编码从gb转为utf-8 unsignature 却不行。于是想看看cocos2d-x库文件的编码格式,发现用的是utf-8&#x…

solaris安装java_Solaris是出色的Java开发平台的原因

solaris安装java几天前,我发布了“ OpenSolaris的死亡:为Java开发人员选择操作系统 ”,其中我说Solaris是Java开发人员的绝佳平台。 这篇文章的重点只是想知道自OpenSolaris淘汰以来我将使用哪个Solaris版本。 正如Neil的评论使我意识到的那样…

正确使用计算机说课稿,《计算机结构原理初步》说课稿

在教师招聘考试的过程中,高中信息说课稿的难度就在于如何处理理论与实践的关系,希望这篇《计算机结构原理初步》说课稿能给予你帮助。各位考官大家好!我是号考生,今天我说课的题目是《计算机结构原理初步》。现代教学理论认为,在教…