When you switch to an activity, an empty activity opens, help to fix it. MainActivity code:

package com.example.chasie.myapp6; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { public static final String ACTION_SECOND_ACTIVITY = "com.example.chasie.myapp6.SecondActivity"; public static final String ACTION_FIRST_ACTIVITY = "com.example.chasie.myapp6.FirstActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button frst_btn = (Button)findViewById(R.id.frst_btn); Button scnd_btn = (Button)findViewById(R.id.scnd_btn); //Button back_btn = (Button)findViewById(R.id.back_btn); frst_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(ACTION_FIRST_ACTIVITY)); } }); scnd_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SecondActivity.class)); } }); } /*public void firstClick(View view) { Intent intent = new Intent(); intent.setClass(this, FirstActivity.class); startActivity(intent); finish(); }*/ } 

AndroidManifest:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.chasie.myapp6"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".FirstActivity" android:label="@string/first" android:parentActivityName=".MainActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/> <intent-filter> <action android:name="com.example.chasie.myapp6.FirstActivity"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> <activity android:name=".SecondActivity" android:label="@string/second" android:parentActivityName=".MainActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/> <intent-filter> <action android:name="com.example.chasie.myapp6.SecondActivity"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> </application> 

Here's what happens when you go through any activity: reality

A must:

expectation

Acttivity_first.xml code:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/first" android:textAppearance="@style/TextAppearance.AppCompat.Title" android:textAlignment="center"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/back" android:id="@+id/back_btn"/> </LinearLayout> 

SecondActivity.java code:

 package com.example.chasie.myapp6; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.PersistableBundle; import android.view.View; import android.widget.Button; /** * Created by chasie on 24.09.16. */ public class SecondActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); setContentView(R.layout.activity_second); //Intent intent = getIntent(); Button back_btn = (Button)findViewById(R.id.back_btn); back_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(SecondActivity.this, MainActivity.class)); } }); } } 
  • It would be better to add the .xml file - E1mir
  • @KryTer_NexT Posted by - chasie
  • 3
    such a call is wrong. startActivity(new Intent(ACTION_FIRST_ACTIVITY)); . It will not work as it should. Show code SecondActivity.java - Vladyslav Matviienko
  • @metalurgus Code added, I tried different ways, none works, as you can see SecondActivity is called differently, but the effect is the same - chasie
  • one
    @metalurgus And why doesn't it work, does he have an intent-filter on this manifest in the manifest? - Kirill Stoianov

1 answer 1

It is hard to say why the first acivity does not start for you, because you only showed the markup, but the second activity seems to display a white screen for you due to the onCreate method

in SecondActivity.java try replacing this:

 public class SecondActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); setContentView(R.layout.activity_second); //Intent intent = getIntent(); Button back_btn = (Button)findViewById(R.id.back_btn); back_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(SecondActivity.this, MainActivity.class)); } }); } } 

on this:

 public class SecondActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //Intent intent = getIntent(); Button back_btn = (Button)findViewById(R.id.back_btn); back_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(SecondActivity.this, MainActivity.class)); } }); } } 
  • one
    Thank you, your decision helped. A little googling, I understood why my Activity not displayed. It was not displayed, because in PersistableBundle persistentState was null by default and therefore nothing was displayed - chasie