Open Activity by clicking in the menu button- Android Studio - android

I am a beginner, I am a few days with this problem
I didn't find a solution. I have a menu that appears in an activity and when I click, I want it to open a new activity.
My question is, what to put in the activity with menu, and what to put in the new activity?
This is my code
Menu_chat.xml (my menu)
android:id="#+id/salva_vida"
android:icon="#drawable/salva_vida"
android:title="#string/save_life"
app:showAsAction="always" />
ChatActivity.java (this is the activity with menu)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.salva_vida:
??????? (What put here?)------------------
break;
tab2.java (this is the new activity- I want to open this)
public class tab2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab2);
}
}

Intent mIntent = new Intent(this, tab2.class);
startActivity(mIntent);
Your ChatActivity.java will look like this :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.salva_vida:
//Start Activity here
Intent mIntent = new Intent(this, tab2.class);
startActivity(mIntent);
break;

Intent mIntent = new Intent(this, tab2.class); startActivity(mIntent);
#Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case android.R.id.home: onBackPressed(); return true; case R.id.salva_vida: //Start Activity here Intent mIntent = new Intent(this, tab2.class); startActivity(mIntent); break;
Don't forget to add your tab2 activity to manifest.

Related

Passing Serializable Class To Parent Activity with Support Action Bar

I am attempting to pass an instance of a serializable class to a parent activity when the user clicks on the up navigation button in the support action bar. The activity in which I am having trouble was started with startActivityForResult() so I can successfuly pass the instance through an intent with the onBackPressed() method overwritten like this
#Override
public void onBackPressed() {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putSerializable("world_key", world);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
but I am lost when attempting to run the same code when the user clicks on the up navigation on the support action bar in my activity
for extra clarity on what I am trying at the moment, I have tried the following, which has not worked
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toolbar:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putSerializable("world_key", world);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
return super.onOptionsItemSelected(item);
}
And insight would be greatly appreciated
You need to use android.R.id.home like the following:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// send bundle here.
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Base Activity for custom ListView menu

I'm new in Android programming. I'm working on an application that have multiple activities. I've created a custom menu with ListView. I would like to put this menu in a base activity to be available in all activities. How should I do this?
Till now, I have something like this:
This is for the button to toggle the menu
menuToggelIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Hide layouts if VISIBLE
if(menuLayout.getVisibility() == View.VISIBLE)
{
menuLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
menuLayout.setVisibility(View.VISIBLE);
}
}
});
And this is for the menu
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = menuArray[position];
Context context = getApplicationContext();
switch (name) {
case "CASE1":
Intent case1Intent = new Intent(context, Activity1.class);
startActivity(case1Intent);
break;
case "CASE2":
Intent case2Intent = new Intent(context, Activity2.class);
startActivity(case2Intent);
break;
case "CASE3":
Intent case3Intent = new Intent(context, Activity3.class);
startActivity(case3Intent);
break;
case "CASE4":
Intent case4Intent = new Intent(context, Activity4.class);
startActivity(case4Intent);
break;
case "CASE5":
Intent case5Intent = new Intent(context, Activity5.class);
startActivity(case5Intent);
break;
case "CASE6":
Intent case6Intent = new Intent(context, Activity6.class);
startActivity(case6Intent);
break;
case "CASE7":
Intent case7Intent = new Intent(context, Activity7.class);
startActivity(case7Intent);
break;
default:
break;
}
}
});
Android custom menu
make one BaseActivity class and all activity extends by BasyActivity class.
BaseActivity class define your main things that show all the screen like menu and other thing. for example
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.manu_file_name, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.icon) {
Toast.makeText(getApplicationContext(), "Hello World", 0).show();
}
return super.onOptionsItemSelected(item);
}
}
and this activity extends all other activity.

Multiple Activities and Up Button

I have an activity that can be asked to run after clicking buttons on many different activities and hence it does not have a "single parent". Therefore in the android manifest I cannot define its parent so I cant get the "Up" button to function properly.
Is there a way I can have the "up" button return to the activity that called it?
You can pass ComponentName of starting activity as an extra
intent = new Intent(this, UpButtonActivity.class);
intent.putExtra(EXTRA_PARENT_COMPONENT_NAME, new ComponentName(this, ThisActivity.class));
startActivity(intent);
The Activity with up button
private ComponentName parent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parent = getIntent().getParcelable(EXTRA_PARENT_COMPONENT_NAME);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getId()) {
case android.R.id.home:
if (parent != null) {
final Intent parentIntent = new Intent();
parentIntent.setComponentName(parent);
startActivity(parentIntent);
finish();
return true;
} else {
return super.onMenuItemSelected(featureId, item);
}
//...
}
}

onActivityResult is not called when the back button in ActionBar is clicked

Here is my problem:
Create a MainActivity. Add a button which will start another activity SecondActivity.
Intent i = new Intent(getActivity(),SecondActivity.class);
startActivityForResult(i,0);
Inside the SecondActivity, I capture the back button click event and also add a button to return to the first Activity.
When back button in action bar is clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// back button
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
//finish();
return false;
}
return super.onOptionsItemSelected(item);
}
When the button inside activity is clicked:
Button btn = (Button)this.findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
The onActivityResult in MainActivity is called when I click the button inside the SecondActivity, but it's never been called if I click the back button in Actionbar of SecondActivity. Can anybody tell me why? Thanks
Here is the code which is working:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// back button
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I guess the finish() will close the current Activity, and return true inform that action has been processed. (The default back action seems to be different from finish().)
Try this:-
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Good answer is Gopal Rao code in the same question. Its worked for me. This is a copy of his solution:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent result = new Intent((String) null);
result.putExtra("SOME_CONSTANT_NAME", true);
setResult(RESULT_OK, result);
finish();
return true;
}
else {
return super.onOptionsItemSelected(item);
}
}

Android Buttons open up the wrong activities

I have three buttons on my main page. Something weird happens when I try to click on them. For example, when I click on the NewGame button, it first displays what the scores button should display, and then if I click the back button it will proceed to display the activity that it was meant to. With the About button, I have to click back twice (it displays the newGame activity and the scores activity. Is there a reason why this is happening?
public class Sakurame extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//set up click listeners for buttons
View HighScoreButton = findViewById(R.id.highscore_button);
HighScoreButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
// More items go here (if any)
}
return false;
}
public void onClick(View v){
switch(v.getId()){
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
case R.id.new_button:
Intent i2 = new Intent(this, HighScore.class);
startActivity(i2);
case R.id.highscore_button:
Intent i3 = new Intent(this, DisplayScores.class);
startActivity(i3);
//break;
// more buttons go here (if any)
}
}
Try adding break; after each startActivity within the onClick method.
Edit to clarify. This ensures that once the case has been met, the switch statement is broken from instead of moving on to the next case statement.
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.new_button:
Intent i2 = new Intent(this, HighScore.class);
startActivity(i2);
break;
case R.id.highscore_button:
Intent i3 = new Intent(this, DisplayScores.class);
startActivity(i3);
break;

Categories

Resources