温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Activity横竖屏切换的问题

发布时间:2020-06-06 23:40:04 来源:网络 阅读:615 作者:祝你幸福365 栏目:移动开发

Activity在横竖屏切换的时候会重新走生命周期的方法,这样做的话会导致一些问题 比如我们在界面上录入的一些数据,但因为重新走了生命周期的方法onCreate()方法,这样就会导致前功尽弃,所以就想办法,在横竖屏切换的时候不能让其重新OnCreate(),Android中我们可以在清单文件中对应的Activity使用如下的属性  android:configChanges="keyboardHidden|orientation|screenSize"  这样就可以避免此类事情的发生。下面是示例代码:

package com.minimax.demo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; ///Activity横竖屏切换/src/com/minimax/demo/MainActivity.java public class MainActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {	super.onCreate(savedInstanceState);	setContentView(R.layout.activity_main);	System.out.println("onCreate().....");	}	@Override	protected void onStart() {	// TODO Auto-generated method stub	super.onStart();	System.out.println("onStart().....");	}	@Override	protected void onResume() {	// TODO Auto-generated method stub	super.onResume();	System.out.println("onResume().....");	}	@Override	protected void onPause() {	// TODO Auto-generated method stub	super.onPause();	System.out.println("onPause().....");	}	@Override	protected void onStop() {	// TODO Auto-generated method stub	super.onStop();	System.out.println("onStop().....");	}	@Override	protected void onDestroy() {	// TODO Auto-generated method stub	super.onDestroy();	System.out.println("onDestroy().....");	}	@Override	protected void onRestart() {	// TODO Auto-generated method stub	super.onRestart();	System.out.println("onRestart().....");	}	@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;	} } 在没有添加对应的属性之前,我们切换横竖屏之后打印的Log日志如下: 04-22 21:50:51.954: I/System.out(24476): onCreate()..... 04-22 21:50:51.964: I/System.out(24476): onStart()..... 04-22 21:50:51.964: I/System.out(24476): onResume()..... 04-22 21:50:56.524: I/System.out(24476): onPause()..... 04-22 21:50:56.524: I/System.out(24476): onStop()..... 04-22 21:50:56.524: I/System.out(24476): onDestroy()..... 04-22 21:50:56.614: I/System.out(24476): onCreate()..... 04-22 21:50:56.614: I/System.out(24476): onStart()..... 04-22 21:50:56.614: I/System.out(24476): onResume()..... 添加如下属性:         <activity             android:name="com.minimax.demo.MainActivity"             android:label="@string/app_name"              android:configChanges="keyboardHidden|orientation|screenSize"              >             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity> 在添加属性之后之后,打印的Log日志如下: 04-22 21:52:25.984: I/System.out(30283): onCreate()..... 04-22 21:52:25.984: I/System.out(30283): onStart()..... 04-22 21:52:25.994: I/System.out(30283): onResume()..... 无论如何我们切换横竖屏,都不会重新走onCreate()方法


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI