I have a problem while hitting the back button on an activity, in particular, the back button on the activity closes the application and destroys all the activities, I want to go back in the stack, and in the previously called activity when hitting the back button.
Here is my code:
ChatToolBar = (Toolbar) findViewById(R.id.chat_bar_layout);
setSupportActionBar(ChatToolBar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View action_bar_view = layoutInflater.inflate(R.layout.chat_custom_bar, null);
actionBar.setCustomView(action_bar_view);
My AndroidManifest.xml:
<activity
android:name=".ChatActivity"
android:parentActivityName=".ProfileActivity" />
I also tried:
<activity
android:name=".ChatActivity"
android:parentActivityName=".ProfileActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ProfileActivity" />
</activity>
Where should be the problem?
I just solved it with this code:
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item)
{
int id = item.getItemId();
if (id == android.R.id.home)
{
SendUserToMainActivity();
}
return super.onOptionsItemSelected(item);
}
private void SendUserToMainActivity()
{
Intent mainIntent = new Intent(ChatActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(mainIntent);
finish();
}
you can add button back in action bar
in mainfest.xml
<activity .ActivityB" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ActivityA" />
Related
I am creating an app where a user can generate a random number from 1 to 90 to play bingo or tambola. Here I set the homeAsUpEnabled as true, and in the Manifest, I have added the support parent activity:
this is the main activity:
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setTitle("Full House: Housie Number Generator");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
ivBoard.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent intent = new Intent(NumberActivity.this, com.app.fullhouse.Board.class);
intent.putExtra("doneNumbers", doneNumbers);
startActivity(intent);
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.reset:
new AlertDialog.Builder(this)
.setTitle("Reset")
.setMessage("Press OK to reset Board")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1) {
recreate();
}
}).create().show();
break;
case R.id.showNoOrder:
Intent intent = new Intent(this, com.app.fullhouse.NumberOrder.class);
intent.putExtra("doneNumbers2", doneNumbers2);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
and this is my other activity:
ActionBar actionBar = getSupportActionBar();
tvnumbers = findViewById(R.id.tvNumbers123);
assert actionBar != null;
actionBar.setTitle("Sequence of Number generated");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
and part of another:
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Board");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
please take into notice that I have cut short much of the unrequired items because the code would get too long
What have I tried:
I tried to startActivityForResult and their end activity by guessing that the back button could be
android.R.id.home
when I try to press the back button on the phone, noting goes wrong and the previous data is still there
but when I press the back button in any of the two-child activities, it goes back, but it recreates the whole activity, hence the game is lost
here is the manifest in case you need it :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.fullhouse">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/fullhouse"
android:label="#string/app_name"
android:roundIcon="#mipmap/fullhouse"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.DayNight.DarkActionBar">
<activity android:name=".GenerateTicket"></activity>
<activity android:name=".NumberOrder">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".NumberActivity" />
</activity>
<activity android:name=".NumberActivity"></activity>
<activity android:name=".Board">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".NumberActivity" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
as well as the menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/reset"
android:menuCategory="container"
android:orderInCategory="104"
android:title="#string/reset_board"
app:showAsAction="never" />
<item
android:id="#+id/showNoOrder"
android:title="Show sequence of numbers"
app:showAsAction="never"
android:orderInCategory="105"/>
</menu>
thank you in advance
Possibly you can find your answer here
How to come back to First activity without its onCreate() called
And can try below from here
Two cases:
If you want to keep other activities live and just want to bring A to front, just use intent flag FLAG_ACTIVITY_REORDER_TO_FRONT.
If you don't want to clear all activities and want an existing instance of A at the top, then use intent flags
FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP.
Note: If you use only FLAG_ACTIVITY_CLEAR_TOP, then onCreate will be
called.
I have found an answer (to all the people who visit this later on :))
the answer is this
Intent intent = new Intent(this, Activity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_FROM_BACKGROUND);
startActivity(intent);
and in the manifest file, you don't need to add anything
cause here it's working for me
tabbed activity overrides by activity when follow android tabbed activity.
I am a newbie in android. I am learning to include some activities on different tabs. however, the activity gets fullscreen and override the main tab
// TabMain.java
public class TabMain extends AppCompatActivity {
.......
}
// SectionsPagerAdapter.java extends FragmentPagerAdapter
#Override
public Fragment getItem(int position) {
switch (position){
case 1:
FragmentTab1 tab1 = new FragmentTab1();
return tab1;
default:
return PlaceholderFragment.newInstance(position + 1);
}
}
public class FragmentTab1 extends Fragment {
#Override
public View onCreateView(
#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.galery_main_layout, container, false);
Intent intent = new Intent(getActivity(), UploadMain.class);
getActivity().startActivity(intent);
return root;
}
}
the UploadMain.java implement Activity
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<activity
android:name=".TabMain"
android:label="#string/title_activity_tab_main"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".FdActivity"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UploadMain"
android:configChanges="keyboardHidden|orientation"
>
</activity>
</application>
I am expecting for the upload main class to be inside tab2
TabMain
| Tab 1 (other activity)
| Tab 2 (UploadMain)
You can't have a Activity inside a ViewPager, it must be a fragment.
And also, when you start activity, like this,startActivity(intent) it will not replace the previous screen or activity, it will just create a new activity with a new layout, having the previous activity paused (and also sometimes stopped) until you close the previous activity. You need to learn more about activity and fragment lifecycles
Your FragmentTab1 starts a new Activity instead of only replacing the View inside your framelayout you start a whole new Activity. Your Code belongs inside the class Fragmenttab1 and the line
Intent intent = new Intent(getActivity(), UploadMain.class);
getActivity().startActivity(intent);
should be removed
I am only a day old in using android studio. I have to create an app for my school project, i did that, i programmed it in android studio and it had no errors. Please help,
this is the Main activity named frontpagee
package com.example.lenovo.shop_easy;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class frontpagee extends Activity
{
public void sendMessage(View view){
Intent intent = new Intent(frontpagee.this, catalog.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_frontpagee, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
the XML goes like this
i want to move from this activity to the second ACTIVITY which is named Catalog
java is
package com.example.lenovo.shop_easy;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class catalog extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_catalog, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view) {
Intent intent = new Intent(catalog.this, product1.class);
startActivity(intent);
}
public void sendMessage1(View view) {
Intent intent = new Intent(catalog.this, PRODUCT2.class);
startActivity(intent);
}
public void sendMessage2(View view) {
Intent intent = new Intent(catalog.this, PRODUCT3.class);
startActivity(intent);
}
public void sendMessage3(View view) {
Intent intent = new Intent(catalog.this, product4.class);
startActivity(intent);
}
public void sendMessage4(View view) {
Intent intent = new Intent(catalog.this, product5.class);
startActivity(intent);
}
public void sendMessage5(View view) {
Intent intent = new Intent(catalog.this, product6.class);
startActivity(intent);
}
public void sendMessage6(View view) {
Intent intent = new Intent(catalog.this, product7.class);
startActivity(intent);
}
public void sendMessage7(View view) {
Intent intent = new Intent(catalog.this, product8.class);
startActivity(intent);
}
public void sendMessage8(View view) {
Intent intent = new Intent(catalog.this, product9.class);
startActivity(intent);
}
public void sendMessage9(View view) {
Intent intent = new Intent(catalog.this, product10.class);
startActivity(intent);
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".frontpagee"
android:background="#drawable/back">
<ImageView
android:layout_width="472dp"
android:layout_height="500dp"
android:id="#+id/imageView11"
android:src="#mipmap/cover"
android:layout_gravity="center_horizontal|top"
android:contentDescription="#string/content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text12"
android:id="#+id/button11"
android:layout_gravity="center_horizontal|bottom"
android:textSize="#dimen/size2"
android:clickable="true"
android:onClick="sendMessage"/>
</FrameLayout>
the code is correct says android studio but when i run the adb on my phone it installs the app, everything goes fine but when i open the app it displays nothing but only the app icon and name at the top and nothing else what to do. please help
and here the main xml file also
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.shop_easy" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".frontpagee"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".catalog"
android:label="#string/title_activity_catalog" >
</activity>
<activity
android:name=".product1"
android:label="#string/title_activity_product1" >
</activity>
<activity
android:name=".PRODUCT2"
android:label="#string/title_activity_product2" >
</activity>
<activity
android:name=".PRODUCT3"
android:label="#string/title_activity_product3" >
</activity>
<activity
android:name=".product4"
android:label="#string/title_activity_product4" >
</activity>
<activity
android:name=".product5"
android:label="#string/title_activity_product5" >
</activity>
<activity
android:name=".product6"
android:label="#string/title_activity_product6" >
</activity>
<activity
android:name=".product7"
android:label="#string/title_activity_product7" >
</activity>
<activity
android:name=".product8"
android:label="#string/title_activity_product8" >
</activity>
<activity
android:name=".product9"
android:label="#string/title_activity_product9" >
</activity>
<activity
android:name=".product10"
android:label="#string/title_activity_product10" >
</activity>
</application>
</manifest>
what to do
You have not overridden the onCreate(Bundle savedInstance) method in class frontpagee and have not called setContentView(R.layout...) in it.
I am just trying to make my webview app run upon the bootup of an android device. I already followed every single step mentioned in this post however it does not run on device boot up. What am I missing here?
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
Toast.makeText(context, "Start Up", Toast.LENGTH_LONG).show();
Intent serviceIntent = new Intent(context,
MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
} catch (Exception e) {
Toast.makeText(context, "Start Up not possible!", Toast.LENGTH_LONG).show();
}
}
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://www.google.com/");
//Note: To detect when a URL has started and finished loading, use WebViewClient.onPageStarted and WebViewClient.onPageFinished.
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
and here is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aa.webview1" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootUpReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</manifest>
your receiver is out of your application element, it should rather be inside
<receiver android:name=".BootUpReceiver"> // i am suppose to be a child of the application
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application> // clossing application element
for more info check here , and here
I have an Activity (MainActivity) with a search button in the action bar. It searches by some input string and shows the results in a ListView in another Activity (SearchResultsActivity). The user might click in any result to select it.
I want to return the value selected by the user to the main activity, but it's not working. I looked in the documentation but I didn't find anything related.
I tried to use setResult(Intent) in the results activity but the onActivityResult() from the main activity never gets called.
What am I doing wrong? How can I do it?
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="foo.bar" >
<!-- ... -->
<uses-sdk android:minSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<!-- Main Activity -->
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main">
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Search results activity -->
<activity android:name=".SearchResultsActivity"
android:parentActivityName=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
</application>
</manifest>
Menu.xml:
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity">
<!-- Search -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
<!-- ... -->
</menu>
MainActivity:
public class MainActivity extends FragmentActivity {
// ...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.activity_main_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// never gets called!
super.onActivityResult(requestCode, resultCode, data);
}
// ...
}
SearchResultsActivity:
public class SearchResultsActivity extends Activity {
private ListView listResults;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_results);
// get the action bar
ActionBar actionBar = getActionBar();
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
listResults = (ListView) findViewById(R.id.listResults);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
/**
* Handling intent data
*/
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
final Serializable[] results = find(query);
listResults.setAdapter(new ArrayAdapter<BusLine>(this, R.layout.list_view, busLines));
listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
setResult(RESULT_OK, new Intent().putExtra("result", selectedResult));
finish();
}
});
}
}
}
I can understand why you might think that you would use setResult and onActivityResult to handle clicks in your search results activity, but that is not how it works. Only when you launch an activity with startActivityForResult do these functions apply -- not when displaying search results.
The solution is to call startActivity on your MainActivity in the normal fashion, that is, not using activity results. Something like this:
listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
Intent intent = new Intent();
intent.putExtra("result", selectedResult);
intent.setClass(SearchResultsActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
Now you will also need to capture this search result in your MainActivity, although you need to make sure you handle the normal case as well as the search results case:
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if (intent.hasExtra("result")) {
// Launched from search results
Serializable selectedResult = (Serializable) intent.getSerializableExtra("result");
...
}
...
}