日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2023/12/2 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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" />

總結

以上是生活随笔為你收集整理的android 重新启动应用程序,通过单击应用程序图标打开Android应用程序时重新启动...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。