How to prevent the MainActivity is opened twice in Android? -
i searched solution more week situation , have not found solution.
i have main activity , use fragments. in project added splash, finishes executing program opens twice.
here's code:
mainactivity.java
package org.jotadev.nh; import org.jotadev.nh.adapter.tabspageradapter; import android.app.actionbar; import android.app.actionbar.tab; import android.app.fragmenttransaction; import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v4.view.viewpager; import android.util.log; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.widget.toast; public class mainactivity extends fragmentactivity implements actionbar.tablistener { private viewpager viewpager; private tabspageradapter madapter; private actionbar actionbar; // tab titles private string[] tabs = { "buscar", "historial", "favoritos", "planes" }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main);//activity_main log.i("mainactivity", "entró en oncreate"); // initialization viewpager = (viewpager) findviewbyid(r.id.pager); actionbar = getactionbar(); madapter = new tabspageradapter(getsupportfragmentmanager()); viewpager.setadapter(madapter); actionbar.sethomebuttonenabled(false); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // adding tabs (string tab_name : tabs) { actionbar.addtab(actionbar.newtab().settext(tab_name) .settablistener(this)); } /** * on swiping viewpager make respective tab selected * */ viewpager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpageselected(int position) { // on changing page // make respected tab selected actionbar.setselectednavigationitem(position); log.i("mainactivity", "entró en onpageselected"); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpagescrollstatechanged(int arg0) { } }); }); } @override public void ontabreselected(tab tab, fragmenttransaction ft) { } @override public void ontabselected(tab tab, fragmenttransaction ft) { // on tab selected // show respected fragment view viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tab tab, fragmenttransaction ft) { } //menu @override public boolean oncreateoptionsmenu(menu menu){ super.oncreateoptionsmenu(menu); menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu, menu); return true; } public void lanzarabout(){ intent intent = new intent(this, about.class); startactivity(intent); } @override public boolean onoptionsitemselected(menuitem item){ switch (item.getitemid()){ case r.id.action_about: lanzarabout(); break; case r.id.action_settings: intent intent = new intent(this, preferences.class); startactivity(intent); break; case r.id.action_show_preferences: log.i("action", "mostrando ajustes"); mostrarpreferencias(); break; case r.id.action_exit: finish(); } return false; } private void mostrarpreferencias() { sharedpreferences pref = getsharedpreferences("org.jotadev.nh_preferences",mode_private); string s = "pantalla completa: " + pref.getboolean("pantallacompleta", false) + ", tamaño letra: " + pref.getstring("tamanoletra", "?") + ", color fondo: " + pref.getstring("colorfondo", "?") + "en desarrollo: " + pref.getboolean("endesarrollo", true); toast.maketext(this, s, toast.length_long).show(); } }
activity_main.xml
<android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.viewpager>
androidmanifiest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.jotadev.nh" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="14" android:targetsdkversion="17" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.change_wifi_state"/> <uses-permission android:name="android.permission.change_network_state"/> <uses-permission android:name="android.permission.internet"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" android:debuggable="true" > <!-- splash --> <activity android:name=".splash" android:label="@string/app_name" android:screenorientation="landscape" android:theme="@android:style/theme.notitlebar.fullscreen" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="org.jotadev.nh.mainactivity" android:label="@string/app_name" > <intent-filter> <category android:name="android.intent.category.default" /> </intent-filter> </activity> <activity android:label="@string/app_name" android:name=".about"> </activity> <activity android:label="@string/title_settings" android:name=".preferences"> </activity> </application> </manifest>
i appreciate help. or please share useful link. dificult find answers.
Comments
Post a Comment