简体   繁体   中英

multiple activities and intents in android

Hi guys

i have simple android app with 3 activity A,B,C

A activity is the main activity and B,C are the childes.

A | -> B

A | -> C

i already know how to move to other activity by buttons but i have strange problem when i start the app,i can only go to those activity for first time and when i press back btn to main activity & press button of another activity the app will crush and show this error "Unfortunately, app has stopped."

For example when Main activity (a) is loaded i will click the btn of 2ndActivity (B) and it will open 2nd activity successfuly

But

when i press back system btn and try to click the btn of 3rd activity (c) the app will failed and show error

note that only each activity that click for first time will be lunch successfuly I mean if I click btn of C activity for first time then the activity B wont work and show error

note no error found in LogCat and app run correctly in emulators,problem will happen on some phone like my gs4 not all phone

sorry for my bad English

I need your help ,Thanks

My MainActivityA.java

 public class MainActivityA extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    android.support.v7.app.ActionBar AB = getSupportActionBar();
    AB.hide();
    setContentView(R.layout.activity_mainA);

    ImageButton b1 = (ImageButton) findViewById(R.id.imagebtndb1);
    b1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent i = new Intent(MainActivityA.this, MainActivityB.class);
            startActivity(i);
        }
    });


    ImageButton b2 = (ImageButton) findViewById(R.id.imagebtndb3);
    b2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent i2 = new Intent(MainActivityA.this, MainActivityC.class);
            startActivity(i2);
        }
    });

My MainActivityB

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    android.support.v7.app.ActionBar AB=getSupportActionBar();
    AB.hide();
    setContentView(R.layout.activity_main_activity2);
    WebView webView = (WebView) findViewById(R.id.webView);
    webView.setHorizontalScrollBarEnabled(false);
    webView.setVerticalScrollBarEnabled(false);
    webView.setBackgroundColor(0);
    webView.loadUrl("file:///android_asset/htm.html");



}

my MainActivityC

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    android.support.v7.app.ActionBar AB=getSupportActionBar();
    AB.hide();
    setContentView(R.layout.activity_main_activity4);
}

my manifest

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivityB"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.activityB" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivityC"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.activityC" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

Problem solved

may be this could help someone beginner like me in future

I added this line to My manifest

<activity 
            android:parentActivityName=".MainActivity" >

<meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.myandroid.app.MainActivity" />
</activity>

Thanks Guys

Have you tried changing your intent filter in your manifest to the full path of the classes

eg

intent-filter

     action android:name="<full domain name here>.activityB"

intent-filter

     action android:name="com.myandroid.app.activityB"

try it for both activityB and activityC.

If the back buttons aren't responding correctly you could always override them to go to the parent activity. Here is something you could do:

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent goToParentIntent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(goToParentIntent);
}

Add this code to each of your child activities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM