Issue
Back button stopped to work in only one Activity. I have three activities which have the same parent. But one of them stopped to back (CurrentMovieActivity). I can't figure out why. Manifest seems to be correct.
What's a possible reason for it?
My Manifest:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_movie_white_24dp"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_movie_white_24dp"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.MovieActivity">
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/settings_title"
android:name=".activity.SettingsActivity"
android:parentActivityName=".activity.MovieActivity" />
<activity
android:label="@string/current_title"
android:name=".activity.CurrentMovieActivity"
android:parentActivityName=".activity.MovieActivity" />
<activity
android:label="@string/favourites"
android:name=".activity.FavouritesActivity"
android:parentActivityName=".activity.MovieActivity" />
<provider
android:authorities="com.globallogic.v_holodynskyi.imdbclient"
android:exported="false"
android:name=".database.MovieProvider" />
I also tried to change launch mode and to override onBackPress
. It looks like this, but I can't see any logs:
@Override
public void onBackPressed() {
Log.i(LOG_TAG, "back pressed1");
super.onBackPressed();
Log.i(LOG_TAG, "back pressed2");
}
My "broken" activity:
public class CurrentMovieActivity extends AppCompatActivity {
private static final String LOG_TAG = CurrentMovieActivity.class.getSimpleName();
private ImageView mPoster;
private TextView mMovieTitle;
private TextView mMovieDescription;
private FloatingActionButton mFloatingButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_movie);
initializeVariables();
}
@Override
public void onBackPressed() {
Log.i(LOG_TAG, "back pressed1");
super.onBackPressed();
Log.i(LOG_TAG, "back pressed2");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.current_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favourite:
Intent openFavourite = new Intent(this, FavouritesActivity.class);
startActivity(openFavourite);
break;
default:
break;
}
return true;
}
private void initializeVariables() {
Intent intent = getIntent();
final String title = intent.getExtras().getString(Intent.EXTRA_TEXT + TITLE);
final String overview = intent.getExtras().getString(Intent.EXTRA_TEXT + DESCRIPTION);
final String posterLocation = intent.getExtras().getString(Intent.EXTRA_TEXT + LOCATION);
mPoster = findViewById(R.id.iv_movie_poster_current);
downloadImage(posterLocation, mPoster);
mMovieTitle = findViewById(R.id.tv_movie_title);
mMovieTitle.setText(title);
mMovieDescription = findViewById(R.id.tv_movie_description_current);
mMovieDescription.setText(overview);
mFloatingButton = findViewById(R.id.fb_add_to_favourites);
mFloatingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveMovie(CurrentMovieActivity.this, title, overview, posterLocation, CONTENT_URI);
}
});
}
}
Solution
This is probably because of using android:parentActivityName. When you using the attribute, it makes your code checking for android.R.id.home
in onOptionsItemSelected
method.
You need to check for android.R.id.home
and don't use a default case in the switch like this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case android.R.id.home:
finish(); // call finish and don't use default case.
break;
}
return true;
}
Here is related documentation: Providing Up Navigation
Answered By - ישו אוהב אותך
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.