隐式Intent实现Activity跳转

在AndroidManifest.xml文件中,各个Activity的配置如下:


    
        
        
        
    




    
        
        
        
    




    
        
        
        
    




    
        
        
        
    

ActivityMain的布局文件activity_main.xml如下:




    

    

ActivityMain加载的menu文件menu_activity_main.xml如下:




    

    

    


ActivityMain.java的Button响应代码如下:

public void buttonClick(View v)  //点击各个Button的响应
{
	switch (v.getId())
	{
		case R.id.buttonActivitySub1:
			Intent intent1=new Intent("android.intent.action.ActivitySub1");
			intent1.addCategory("android.intent.category.ActivitySub1");
			startActivity(intent1);
			break;
		case R.id.buttonActivitySub2:
			Intent intent2=new Intent("android.intent.action.ActivitySub2");
			intent2.addCategory("android.intent.category.ActivitySub2");
			startActivity(intent2);
			break;
		case R.id.buttonActivitySub3:
			Intent intent3=new Intent("android.intent.action.ActivitySub3");
			intent3.addCategory("android.intent.category.ActivitySub3");
			startActivity(intent3);
			break;
		default:
			break;
	}
}

ActivityMain.java的item响应代码如下:

public boolean onOptionsItemSelected(MenuItem item)  //点击menu的各个item的响应
{
	switch (item.getItemId())
	{
		case R.id.itemActivitySub1:
			Intent intent1=new Intent();
			intent1.setAction("android.intent.action.ActivitySub1");
			intent1.addCategory("android.intent.category.ActivitySub1");
			startActivity(intent1);
			break;
		case R.id.itemActivitySub2:
			Intent intent2=new Intent();
			intent2.setAction("android.intent.action.ActivitySub2");
			intent2.addCategory("android.intent.category.ActivitySub2");
			startActivity(intent2);
			break;
		case R.id.itemActivitySub3:
			Intent intent3=new Intent();
			intent3.setAction("android.intent.action.ActivitySub3");
			intent3.addCategory("android.intent.category.ActivitySub3");
			startActivity(intent3);
			break;
		default:
			break;
	}
	return super.onOptionsItemSelected(item);
}

   而在ActivitySub1.java文件、ActivitySub2.java文件和ActivitySub3.java文件中,使用隐式Intent跳转到Activity main中,使用的代码如下:

Button buttonBackToActivityMain=findViewById(R.id.buttonBackToActivityMain);
buttonBackToActivityMain.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        Intent intent=new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");
        startActivity(intent);
    }
});

真机测试视频如下:

隐式Intent实现Activity跳转_第1张图片

你可能感兴趣的:(安卓移动开发)