开发者

switch activities

开发者 https://www.devze.com 2023-02-25 03:50 出处:网络
Dear All; I am really confused in the matter of layouts and activities , I was used on windows applications for forms and classes but here it seems a bit deferent , So I don\'t know when i will use a

Dear All; I am really confused in the matter of layouts and activities , I was used on windows applications for forms and classes but here it seems a bit deferent , So I don't know when i will use and layout and when I have to switch activity... any way I have build an sample application and I decided for each activity to have its layout. and I want to switch between activities. so I write the code in the AndroidManifest.XML

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".activity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
         <intent-filter>
             <action android:name="android.content.intent.ActionBootCompleted" />
             <category andro开发者_运维百科id:name="android.content.intent.CategoryDefault" />
         </intent-filter>
    </activity>
     <activity android:name=".activity1"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN2" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".home"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN2" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

and then In each onclickListener :

Button b1=(Button)findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
             //alert.show();
       //  setContentView(R.layout.menu);
         String packageName = activity1.class.getPackage().getName();

       String packageAndClassName = activity1.class.getName();
       Intent intent = new Intent().setClassName(packageName,packageAndClassName);
       startActivity(intent);

        }
        });

So it is switching between activity to Home but When I want to switch from home to actvity1 it returns me back to activity ...

So what is the problem???


This snippet ought to do a better job of launching your activity1. You'll have to rename MyActivity in the example to the name of the activity class hosting the snippet (or provide an alternative context variable).

      Intent intent = new Intent(MyActivity.this, activity1.class);
      startActivity(intent);


from AndroidManifest.XML we can see that activity is your main activity,so

Intent intent = new Intent().setClassName(packageName,packageAndClassName);

will switch to acticity.

try to use

Intent intent = new Intent(HomeActivity.this, activity1.class);

more:

public Intent (Context packageContext, Class<?> cls)

Create an intent for a specific component. All other fields (action, data, type, class) are null, though they can be modified later with explicit calls. This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you; reference


Take a look at the topic Tasks and Back Stack. It explains the concepts of navigating between activities pretty clearly.

0

精彩评论

暂无评论...
验证码 换一张
取 消